I have worksheet which gets data from RTD server through the following formula:
=RTD("tos.rtd", , "ASK", ".SPX150220C750")
I would like to save the worksheet with above formula every 1 minute or so. The challenge is to pause VBA code and also make sure that before we save, the value in the cell is updated. I have tried the following code.
Sub Archiving()
For i = 0 To 4
Worksheets("Test").Activate
Application.Sheets("Test").Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="D:\Save " & i & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Save
ActiveWindow.Close
Windows("Real time data.xlsm").Activate
Application.DisplayAlerts = True
Application.Wait (Now + TimeValue("0:00:05"))
ActiveWorkbook.RefreshAll
DoEvents
Next i
End Sub
The code does not work, simply because DoEvents waits until RTD is done with updates, which is never. I also have seen example where connection to DB is paused explicitly, but I don't know how to adapt it RTD server case. I tried to run RTD server from C#, but failed miserably. RTD in C# for dummies Any suggestions?
I had a similar issue. I added the following command in my VBA to trigger a RTD data refresh. I did this command before I used the data in my VBA macro. Hope this helps.
Excel.Application.RTD.RefreshData
The challenge is to pause VBA code and also make sure that before we save, the value in the cell is updated.
THE PROBLEM with your previous implementation is that by doing it inside a loop, since VBA doesn't support multi-threading, the application was "busy" and unable to receive new data from RTD server.
This is based mostly on what I've gathered from Microsoft's documentation/knowledge-base, emphasis added:
And further suggests that toggling the application's
.CalculationState
etc. will have no effect on the RTD server:So the data will be updated when it becomes available from the server (presumably not a problem) but what is a problem in your implementation is that the workbook can't accept it because it's running the VBA thread and an RTD formula is not a "normal" external link.
I suspect that another dissimilarity is that the
RefreshAll
method has no effect on this function, you can't force it to get external data because it's already doing so when the workbook can accept it.POTENTIAL SOLUTION
By using the
Application.OnTime
event to schedule the save interval, I think you should be able to avoid the problem of the workbook being unable to receive data.if you want to save the data at a regular interval, this function will call itself recursively, subject to the limitations of the
Appliction.OnTime
method:NOTE: I can't replicate on my end because I don't have your COM object /etc. that is being called from the RTD function. So, take this with a grain of salt and understand that I am very limited in how much further assistance I can offer you.