+ Reply to Thread
Results 1 to 9 of 9

Macro works but its Slow

  1. #1
    Registered User
    Join Date
    11-12-2014
    Location
    usa
    MS-Off Ver
    2007
    Posts
    5

    Macro works but its Slow

    Hi all.
    First of all I am a beginner and I am sure there are better ways to do every piece of my macro below.
    Please see and let me know if you have any suggestions to make it work faster.

    Many thanks for your time.

    Sub Macro2()
    Cells(4, 22) = Cells(4, 1)
    Cells(4, 23) = Cells(4, 2)
    i = 5
    j = 4
    Do While Cells(i, 1) = Cells(j, 22) And Cells(i, 2) = Cells(j, 23)
    If Cells(i, 19) > Cells(i, 18) Then
    Cells(j, 24).Value = "C"
    Do While Cells(i, 1) = Cells(j, 22) And Cells(i, 2) = Cells(j, 23)
    i = i + 1
    Cells(2, 27) = i
    Cells(3, 27) = j
    Loop
    If IsEmpty(Cells(i, 1)) Then
    GoTo d:
    End If
    j = j + 1
    Cells(2, 27) = i
    Cells(3, 27) = j
    Cells(j, 22) = Cells(i, 1)
    Cells(j, 23) = Cells(i, 2)
    Else:
    i = i + 1
    Cells(2, 27) = i
    Cells(3, 27) = j
    If IsEmpty(Cells(i, 1)) Then
    GoTo d:
    End If
    If Cells(i, 1) <> Cells(j, 22) Or Cells(i, 2) <> Cells(j, 23) Then
    Cells(j, 24).Value = "UNC"
    j = j + 1
    Cells(j, 22) = Cells(i, 1)
    Cells(j, 23) = Cells(i, 2)
    End If
    End If
    Loop
    d:
    End Sub

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro works but its Slow

    Can you give us a sample data set? Trying to set one up to step through your code is tedious. Also, if you could explain what you are trying to accomplish (if the data set doesn't make that obvious) that would help a lot too.

    But just for starters, something like this is processor intensive:

    Please Login or Register  to view this content.
    You're essentially updating a value in a cell every time through the loop (and I have no idea how many rows/i's you have). Instead, you could just do your incrementation, then put the final value in a cell:

    Please Login or Register  to view this content.
    If you've got thousands and thousands of rows, that would help.

    Anyway, I suspect you're doing some unnecessary looping that is taking a lot of time. Can tell you more with a data set.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Macro works but its Slow

    I agree with walruseggman. The submitted code is very inefficient. It would be more productive to submit a statement demonstrating what you are trying to accomplish along with a sample workbook and have new code written for it.

  4. #4
    Registered User
    Join Date
    11-12-2014
    Location
    usa
    MS-Off Ver
    2007
    Posts
    5

    Re: Macro works but its Slow

    Lower Retail G1 670.8 -369.3
    Lower Retail G1 670.8 2749.9
    White G1 670.8 80.7
    White G1 670.8 -145.0
    White G1 670.8 113.1
    White G1 670.8 -188.4
    White G1 670.8 843.9
    White G1 670.8 1851.2
    White G1 670.8 -809.2
    White G1 670.8 -876.3
    White G1 670.8 1189.5
    White G1 670.8 547.6
    White G1 670.8 -506.9
    White G1 670.8 996.9
    White G1 670.8 799.2
    White G1 670.8 1922.3
    White G1 670.8 -545.0
    White G1 670.8 -799.9
    White G1 670.8 1144.7
    White G1 670.8 618.7
    White G1 670.8 -242.7
    White G1 670.8 1151.5
    Upper Retail G2 670.8 881.1
    Upper Retail G2 670.8 642.8
    Upper Retail G2 670.8 1435.0
    Upper Retail G2 670.8 1034.2
    Upper Retail G2 670.8 1642.3
    Upper Retail G2 670.8 -26.4
    Here is a sample
    First two columns are A and B last two are R and S
    Here is a brief description;
    for a range with similar values in first two cells in columns a and b, compare rows r and s and if s >r record C otherwise record UNC.
    I am creating two columns with unique set of names of the first two columns and placing C or UNC in front of them.
    Cells(2, 27) = i
    Cells(3, 27) = j
    are just useless counters for me to know where I am! ignore them.
    Thanks for your time.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Macro works but its Slow

    Submit an example workbook.

  6. #6
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro works but its Slow

    Ok, so I'm still a bit confused, but i think a lot of that has to do with the massively different approaches we would take to this problem.

    But moreover, your explination in your breif description for UNC doesn't seem to match what's in your code. Where I'm most confused is that it seems you only want to compare S and R values for the first unique instances (rows) of A and B, and ignore all the other rows? Like in your example, you would only do S and R comparison on rows 4, 6, and 26?

    Anyway, here's the pseudo-code, please mark what's wrong.

    Please Login or Register  to view this content.
    Last edited by walruseggman; 11-12-2014 at 05:58 PM.

  7. #7
    Registered User
    Join Date
    11-12-2014
    Location
    usa
    MS-Off Ver
    2007
    Posts
    5

    Re: Macro works but its Slow

    For each value in Column A, starting in Row 4:
    If the value in Column A and Column B Row i haven't been seen before, then:
    1) Add Column A and B values to first blank row in V & W starting with Row 4
    2) Compare R & S Values in Row i, then:
    If S > R in Row i, then:
    put "C" in Column X in the row with the newly added V & W values
    Otherwise
    repeat 2 for the next row with same first two cell values.
    if non of the rows with same first two cell values S > R then put "UNC" in Column X
    repeat this for next set of rows with similar first two cells.


    Many thanks

  8. #8
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro works but its Slow

    Here you go. If it does do what you want it to, I'd be interested to hear from you how much faster it is.

    Please Login or Register  to view this content.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro works but its Slow

    Hi, nn90,

    please have a look at Forum Rule#3 to understand why you are asked to apply code-tags to your procedure in the opening post.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. Expand rows macro works, but is slow. Is there another solution?
    By markppp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2013, 12:23 PM
  2. Replies: 1
    Last Post: 07-15-2011, 01:24 AM
  3. After printing ,the macro works very slow
    By yoav_b in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-05-2007, 04:31 AM
  4. [SOLVED] Code works but is slow except when....
    By dexterslabmi@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2006, 04:35 AM
  5. Code works, but is slow
    By EMoe in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-25-2005, 05:08 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