+ Reply to Thread
Results 1 to 10 of 10

Do while running slow

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2020
    Location
    New Westminster, BC, Canada
    MS-Off Ver
    365 V2111
    Posts
    4

    Smile Do while running slow

    Created a 'do while' code, which runs very slow.

    Below is the labelClass code
    do while.PNG

    Here is the do while code
    dowhile.PNG

    Appreciate any feedback

    Regards
    Margo
    Attached Files Attached Files
    Last edited by margo71; 12-31-2021 at 03:54 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Do while running slow

    What is the activecell when you launch the macro ?
    What is the purpose of the macro ?
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    06-08-2020
    Location
    New Westminster, BC, Canada
    MS-Off Ver
    365 V2111
    Posts
    4

    Re: Do while running slow

    Hi,

    Column M is the active cell.
    The purpose is to let client know the current stock level, so they know what to order.
    Column L will be removed in the final file

    Thank you,
    Margo

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Do while running slow

    See file attached
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-08-2020
    Location
    New Westminster, BC, Canada
    MS-Off Ver
    365 V2111
    Posts
    4

    Re: Do while running slow

    Thank your for your code.
    Your code kept starting from Cell M1. So all I did is adding the expression 'Application.ScreenUpdating = False' - and the code runs in a fraction of a second.

    Merci Beaucoup et bonne année
    Margo

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,139

    Re: Do while running slow

    Maybe this:

    Sub sUpdateStatus()
    
    Dim lLR As Long, i As Long
    Dim lStockCol As Long
    Dim Status As Long
    
    lStockCol = Application.WorksheetFunction.Match("Stock on Hand", Range("A1").EntireRow, 0)
    lLR = Cells(Rows.Count, lStockCol).End(xlUp).Row
    
    Application.ScreenUpdating = False
    For i = 2 To lLR
        Status = Cells(i, lStockCol).Value
        Cells(i, lStockCol + 1).Value = flabelClass(Status)
    Next 'i
    Application.ScreenUpdating = True
    
    End Sub
    
    Public Function flabelClass(Status As Long) As String
    
    Select Case Status
    Case Is = 0
        flabelClass = "Not In Stock"
    Case 1 To 4
        flabelClass = "Very Limited Stock"
    Case 5 To 29
        flabelClass = "limited stock"
    Case Is >= 30
        flabelClass = "In Stock"
    End Select
       
    End Function
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Registered User
    Join Date
    06-08-2020
    Location
    New Westminster, BC, Canada
    MS-Off Ver
    365 V2111
    Posts
    4

    Re: Do while running slow

    Thank you. I just kept my simple codes and statements and only added expression 'Application.ScreenUpdating = False', which solved the problem.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,139

    Re: Do while running slow

    Your code is slow because you select cells. ScreenUpdating = False masks the issue because the screen is not being redrawn. That said, if the volume of data is small, it does resolve your problem (as you state).

    I suspect the original code only appears to be slow because you are watching the active cell move down the screen and the cells being updated.

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Do while running slow

    Merci Beaucoup et bonne année
    Merci, toi aussi

    By the way, you got all explanations from TMS about how slow could be your code, but there is still some improvement to do, working with memory.
    It could be useful for some others,

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,139

    Re: Do while running slow

    Just for fun, this uses arrays so the bulk of the work is done in memory. It locates the stock in hand column, stores the values in an attay, loops through it and creates another array of "labels", and outputs it the next available column. It should be virtually instantaneous.

    Option Explicit
    
    Sub sUpdateStatus_v2()
    
    Dim lNC As Long, lLR As Long, i As Long
    Dim lStockCol As Long
    Dim vStatus, vLabel
    
    lNC = Cells(2, Columns.Count).End(xlToLeft).Column + 1
    lStockCol = Application.WorksheetFunction.Match("Stock on Hand", Range("A1").EntireRow, 0)
    lLR = Cells(Rows.Count, lStockCol).End(xlUp).Row
    
    vStatus = Range(Cells(2, lStockCol), Cells(lLR, lStockCol))
    ReDim vLabel(LBound(vStatus) To UBound(vStatus))
    
    For i = LBound(vStatus) To UBound(vStatus)
        vLabel(i) = flabelClass(vStatus(i, 1))
    Next 'i
    
    Cells(2, lNC).Resize(UBound(vStatus)) = _
        Application.Transpose(vLabel)
    
    End Sub
    
    Public Function flabelClass(Status) As String
    
    Select Case Status
    Case Is = 0
        flabelClass = "Not In Stock"
    Case 1 To 4
        flabelClass = "Very Limited Stock"
    Case 5 To 29
        flabelClass = "limited stock"
    Case Is >= 30
        flabelClass = "In Stock"
    End Select
       
    End Function

+ 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] Workbook running very slow
    By katiedee1625 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-01-2017, 04:46 PM
  2. VBA running slow
    By Toonies in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-25-2013, 04:13 PM
  3. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  4. Macro to hide/unhide cells not running or running slow
    By mbp727 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2013, 04:22 PM
  5. Macro running very slow
    By oo0tommyk0oo in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-22-2011, 03:14 AM
  6. [SOLVED] Really Slow Running of Macro
    By John in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-15-2006, 01:35 PM
  7. [SOLVED] VBA running VERY slow
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2005, 12:05 PM

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