+ Reply to Thread
Results 1 to 2 of 2

For Loop not working; step-by-step does work

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    70

    For Loop not working; step-by-step does work

    Hello all,

    I use the code below to loop through a column of values and an external formula (meaning not provided from excel, an addon I downloaded) to download data for each of those values, and then compile the data in another sheet.

    Sub TopManagerHoldings()
    
    Application.ScreenUpdating = False
    
    Dim x As Integer
    
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet
      Set copySheet = Worksheets("TopHoldings")
      Set pasteSheet = Worksheets("PasteSheet")
    
    
    For x = 2 To 4
    
    Worksheets("TopHoldings").Range("B2:D4000").ClearContents
    
    Worksheets("TopHoldings").Range("K2").Value = Worksheets("Funds").Range("A" & (x)).Value
    
    Application.Wait (Now + TimeValue("0:00:03"))
    
    Application.Calculate
    
    v = Worksheets("TopHoldings").Range("C:C").Cells.SpecialCells(xlCellTypeConstants).Count
    
     copySheet.Range("A2:D" & v).Copy
      pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
      Application.CutCopyMode = False
    
    Next x
    
    End Sub
    When I run the code, no data is downloaded to be copy/pasted into the sheet. However, when I go through the code step-by-step (press f8), then it seems to work fine. Changing the amount of time or use of application.calculate doesn't seem to help. I've also tried changing the forumula auto-calculate feature off and on and screen updating. it seems like whatever I have in the for loop it doesn't iterate through, and does everything at once.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: For Loop not working; step-by-step does work

    Try moving the wait to after the calculate and add a DoEvents line or 2.

    Worksheets("TopHoldings").Range("K2").Value = Worksheets("Funds").Range("A" & (x)).Value
    Application.Calculate
    Doevents
    Application.Wait (Now + TimeValue("0:00:03"))
    Doevents
    Cheers
    Andy
    www.andypope.info

+ 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: 07-11-2019, 12:52 PM
  2. Replies: 3
    Last Post: 07-07-2017, 01:24 PM
  3. Nested For Loop With Step Funtion Not Working
    By cokingtins1 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-05-2017, 04:37 PM
  4. VBA Code works by step by step, doesn't work by running
    By lucasar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2016, 06:33 AM
  5. [SOLVED] Stock Chart apparence is different if Macro1 runs step by step not directly !
    By HerryMarkowitz in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 09-23-2014, 04:31 AM
  6. [SOLVED] For step loop, need step size to change based on reference cell in row
    By Telperion in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-22-2013, 03:41 PM
  7. [SOLVED] loop does not work except in debugging step through mode
    By Nicole B in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2005, 01:06 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