Column Loop, Formula and Wait

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,

I had some help on this earlier from Joe4 but I have reached a sticking point again.

  • I'm dealing with an internal company API that I get data from by using an excel add-in.
  • I put a formula in the cell, it looks at the date and the account plus arguments in the formula and retrieves data right to the cell.
  • Once the number of dates and accounts gets large (1000x2000 for example) something happens and the API data doesn't make it to excel. Maybe because its trying to refresh all those cells periodically.
My planned workaround is to go column by column in a loop, request the data and once it's in the column copy and paste as values to get rid of the formula/api call.

Feeling chuffed that it would work I ended up with the code below. But, it doesn't always work because the API is sometimes faster, sometimes slower. When it is being quick, the first Sub populates the column with the API formula, delays 5 seconds to let the API work and then cuts and pastes only the value before moving on and works great.

My problem is when the API is a little slower.

Is there any way to modify the code below to not just wait a certain amount of time, but to determine whether or not there is anything in the column and IF NOT, WAIT. If so, move on to the next column range.
I'd like to be able to let the application/code decide when the API formula call is done and shows a numeric value.

Any help is appreciated. Mini-sheet below and code although without the API you won't be able to call data.

Thanks!

VBA Code:
Sub LevelDivisionRefMan()
'
    Dim rng As Range
    Dim y As Long
               
y = 2
    
    For Each rng In Range("B2:G2").Columns 'This actually can have almost a thousand columns'

    Range(Cells(2, y), Cells(1260, 2)).Formula2R1C1 = _
        "=DailyMarkAcct(R1C,RC1,Account(),ManufacRef())"
        
    Call DelayFiveSeconds
   
    Range(Cells(2, y), Cells(1260, 2)).Copy
    Range(Cells(2, y), Cells(1260, 2)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    y = y + 1
    
    Next rng

End Sub

Sub DelayFiveSeconds()

    Dim NowTick As Long
    Dim EndTick As Long

    NowTick = Now
    EndTick = Now + TimeValue("00:00:05")

    Do Until NowTick >= EndTick
        DoEvents
        NowTick = Now()
    Loop

End Sub


Cell Formulas
RangeFormula
B2:G12B2=DailyMarkAcct(B$1,$A2,Accout(),ManufacRef())
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top