+ Reply to Thread
Results 1 to 7 of 7

How to quicken a do loop that takes 23 minutes?

  1. #1
    Registered User
    Join Date
    07-14-2017
    Location
    London
    MS-Off Ver
    2010 Excel
    Posts
    3

    Lightbulb How to quicken a do loop that takes 23 minutes?

    Hi all,

    I've recently started using VBA and I've done a relatively simple macro, but it's using Do Loops. When I ran this macro over my entire range (around 4500 cells) it took 23 minutes. My code is below, highlighted in red is the text that I need to ideally change so that my code runs faster. Does anyone have any suggestions?

    -------------------------------------------
    Sub WeekdayColumn()
    '
    ' Add weekday column into incident data
    '

    '
    Sheets("Incident Data").Select
    Dim RowCount As Integer
    RowCount = ActiveSheet.Range("A1").End(xlDown).row
    Range("P1").Select
    ActiveCell.FormulaR1C1 = RowCount

    Application.ScreenUpdating = False

    Range("A1").Select
    Selection.End(xlToRight).Select
    Selection.EntireColumn.ClearContents
    ActiveCell.Offset(1, 0).Range("A1").Select

    Dim i As Integer, StartTime As Date

    StartTime = Now()

    Call WeekdayFormulaDoLoop

    ActiveCell.Offset(-1, 0).Range("A1").ClearContents
    Range("A1").Select
    Selection.End(xlToRight).Select
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Weekday"
    Range("P1").Select
    Selection.ClearContents

    MsgBox Format(Now() - StartTime, "hh:mm:ss")

    End Sub

    Sub WeekdayFormulaDoLoop()
    '
    ' WeekdayFormula Macro
    '

    Do Until ActiveCell.Offset(0, -1) = ""
    For i = 1 To Range("P1")
    Application.StatusBar = "Rows to Process..." & Range("P1") - i

    ActiveCell.FormulaR1C1 = "=WEEKDAY(RC[-12],2)"
    ActiveCell.Offset(1, 0).Range("A1").Select

    Next i

    Loop


    Application.StatusBar = False

    ActiveCell.ClearContents

    End Sub
    -------------------------------------------

    Thanks!

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: How to quicken a do loop that takes 23 minutes?

    Speed of code will depend on multiple factors.

    First, try setting Calculation to Manual at start of your code. And set it to Automatic at end.
    Please Login or Register  to view this content.
    It would help, if you can upload sample workbook that mirrors your actual set up, but with less rows of data.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,754

    Re: How to quicken a do loop that takes 23 minutes?

    Hi lyndsey4
    Welcome to the Forum.
    In future please view the forum rules before posting. Your post does not comply with rule #3.
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the [#] button at the top of the post window.
    With that been said...please upload a sample of your file...
    To attach a file, click on Go Advanced (below the Edit Window) while you are composing a reply, then scroll down to and click on Manage Attachments and the Upload window will open. Click on Browse and navigate to (and double-click) the file icon that you want to attach, then click on Upload and then on Close this Window to return to the Edit window. When you have finished composing your post, click on Submit Post.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How to quicken a do loop that takes 23 minutes?

    In addition to setting the calculation mode to manual and not selecting everything before processing - you could try this instead of the loop in Sub WeekdayFormulaDoLoop

    Please Login or Register  to view this content.
    That would be the entire content of that procedure assuming the correct initial cell was selected first. The value in range P1 was decreased as you seem to count all used rows but start processing in Row 2 so the value in P1 is overstated by 1.

    There are other things than can speed this up but nothing as effective as removing the loop.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: How to quicken a do loop that takes 23 minutes?

    You can apply the formula to an entire range all at once. Selecting each cell and doing it one at a time takes a lot longer.

    This will apply the formula to the active cell down to the last used row in the adjacent column .

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  6. #6
    Registered User
    Join Date
    07-14-2017
    Location
    London
    MS-Off Ver
    2010 Excel
    Posts
    3

    Re: How to quicken a do loop that takes 23 minutes?

    Thanks so much, this worked perfectly!!

  7. #7
    Registered User
    Join Date
    07-14-2017
    Location
    London
    MS-Off Ver
    2010 Excel
    Posts
    3

    Re: How to quicken a do loop that takes 23 minutes?

    Quote Originally Posted by AlphaFrog View Post
    You can apply the formula to an entire range all at once. Selecting each cell and doing it one at a time takes a lot longer.

    This will apply the formula to the active cell down to the last used row in the adjacent column .

    Please Login or Register  to view this content.
    This worked perfectly, thank 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. Replies: 1
    Last Post: 03-22-2015, 06:59 PM
  2. *.XLS file takes 30 minutes to open
    By electro-89 in forum Excel General
    Replies: 3
    Last Post: 01-19-2015, 09:07 AM
  3. It takes 1-2 minutes to respond when changing a value of cell
    By jayce_sos in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2014, 03:00 AM
  4. VBA Code takes 10 minutes of crunch time to return result.
    By jaimeteele in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-28-2013, 02:19 PM
  5. Excel takes 15 minutes to change an option
    By cloke in forum Excel General
    Replies: 4
    Last Post: 02-11-2013, 07:15 AM
  6. Slow for each check takes 6 minutes
    By wolfen in forum Excel General
    Replies: 2
    Last Post: 03-02-2012, 10:33 AM
  7. [SOLVED] File takes 40 minutes to load (auto-calculation)
    By R L in forum Excel General
    Replies: 1
    Last Post: 01-26-2005, 07:06 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