+ Reply to Thread
Results 1 to 7 of 7

Loop + "or"

  1. #1
    Registered User
    Join Date
    07-20-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    17

    Loop + "or"

    Hello,

    I am new to VBA and my approach is usually through trial and error and hope that it will work.

    Unfortunately this time I am stuck and I hope you could help me.


    I have created formula as below in cell H3:

    '=IF((IF(OR(C3=0;B3=0;D3=0;E3=0;F3<=0);0;A4- A3))<0;0;IF(OR(C3=0;B3=0;D3=0;E3=0;F3<=0);0;A4-A3))

    Then I have double clicked and the formula was copied down the column.
    Calculations were performed within 10min and I got what I wanted.


    I have read that using VBA to perform calculations will speed up calculations.
    I have created macro as below which works on small number of cells but for the large number of rows it takes too much time.

    I have estimated that the same calculations as above in my case with the loop will take 12h.

    Can you please have a look and let me know what I am doing wrong?
    I presume the problem is with the “or” function.

    I am looking forward for any suggestions.

    Flooyd

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: Loop + "or"

    Hi,

    you can try to disable some excel functions to speed up the process

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-20-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    17

    Re: Loop + "or"

    hi,

    yes, I have tried this, but not much better

  4. #4
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Loop + "or"

    I don't know why but it seems to me you have a problem with the original formula:

    =IF((IF(OR(C3=0;B3=0;D3=0;E3=0;F3<=0);0;A4- A3))<0;0;IF(OR(C3=0;B3=0;D3=0;E3=0;F3<=0);0;A4-A3))
    if you already check if OR(C3=0;B3=0;D3=0;E3=0;F3<=0) in the TRUE part of the formula, why do you check it second time in the OR(C3=0;B3=0;D3=0;E3=0;F3<=0)
    ??
    If the BLUE condition meets then the RED one is redundant.
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  5. #5
    Registered User
    Join Date
    07-20-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    17

    Re: Loop + "or"

    hi,

    I agree the formula is too complex, but works as I have been using it for a while and gives the results I am looking for.

    I have got 7 columns:
    A contains time stamp
    B to F values

    Column H should check value in cells B to F. As long as the logical test is true, than blank(“”). If the logical test is false it should subtract time form column A.
    B to F logical test
    B=0
    C=0
    D=0
    E=0
    F<0

    I think this is covert by the VBA if

    If Cells(i, 2) = 0 Or Cells(i, 3) = 0 Or Cells(i, 4) = 0 Or Cells(i, 5) = 0 Or Cells(i, 6) < 0 Then Cells(i, 8).Value = "" Else Cells(i, 8).Value = Cells(i + 1, 1).Value - Cells(i, 1).Value
    On Error Resume Next

    All the results are saved in column H.
    Depends on quality of data I am receiving the subtract of time gives negative number, in order to avoid it I have created the VBA if, which looks for negative numbers and replace them with blank (“”)


    If Cells(i, 8) < 0 Then Cells(i, 8).Value = ""

    I think the loop approach is correct, please correct me if I am wrong.

    Is it possible to replace the long expression:

    If Cells(i, 2) = 0 Or Cells(i, 3) = 0 Or Cells(i, 4) = 0 Or Cells(i, 5) = 0
    to something like this:

    If Cells(i, j) = 0
    If yes, would it help and reduce time?


    Flooyd

  6. #6
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Loop + "or"

    You could try using COUNTIF
    The code would be something like:
    Please Login or Register  to view this content.
    but from my experience the worksheet functions are faster. So if you could do it without code... your choice.

  7. #7
    Registered User
    Join Date
    07-20-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    17

    Re: Loop + "or"

    Bulina2k,

    Thank you for your tip, I will use this solution for a different case.

    I kept digging and I came up with code like the one below. It works and is 8x faster than the worksheet function. (at least in this case)

    flooyd



    Please Login or Register  to view this content.
    Last edited by flooyd; 08-12-2016 at 10:00 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy cells from range "C3" to "F3" with loop and blank field
    By masterm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-16-2015, 01:03 PM
  2. Replies: 1
    Last Post: 11-04-2015, 04:34 AM
  3. [SOLVED] How can I change "A" to "B" using a loop or operation?
    By nim73n in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2014, 03:48 AM
  4. [SOLVED] Replace multiple "If Then" with "For" loop
    By checkltout in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2014, 10:56 AM
  5. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  6. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  7. [SOLVED] Loop to check for "yes" then copy the IDs with "yes" and paste to other workbook
    By Hallet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-11-2012, 12:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1