2

I'm subscribed to a RTD financial application with the option to export real time data to Excel via DDE. So, using the DDE copy/paste app option, I've created a Excel table with real time DDE financial data. Cells actually contains DDE formulas. Now I open, refresh and save table manually, but what I need is a solution that will open, refresh the data, save and close this excel table automatically.

I didn't now how to attach the file, so here is a screenshot of it. My Excel DDE file

Since I'm new to vba and vbscript, I was searching internet for a solution but without any success, especially not for automatic update (refreshment) of the DDE data.

What I've been doing for the past few days, was searching, trying and customising different kind of vba macros and vbscripts, and today I'm completely lost and confused.

I have tried bunch of suggested solution, even yours Refresh data and exit with saving Macro Excel and On workbook open, Excel Macro to refresh all data connections sheets and pivot tables and then export the pivot to csv but in my case they don't work.

Any solution, reference, code example will be greatly appreciated.

Community
  • 1
  • 1

3 Answers3

0

Kindly try the below one and be careful when using

Application.Calculation = xlCalculationManual Application.Calculation = xlCalculationAutomatic

this sets the entire excel application to calculate formula's either automatically or manually. If you use Application.Calculation = xlCalculationManual

you'll notice your automatic formulas no longer work.

mulla
  • 143
  • 1
  • 11
0

Thanks for the reply @mulla, but this doesn't work. In vbscript I get runtime error and in vba nothing happens (does not affect refreshment). The same thing is for the Worksheet.Calculate.

Than I recorded a macro upon manual update of the links (data) in order to inspect how Excel proceed update and this actually works, but not all the time. If I open Workbook manually and I keep it opened than whole export works perfectly since the data is constantly refreshed (not what I need). In case when the Workbook is opened via vbscript only (what I need), as you can see in the output below some of the values are exported successfully (for the faster refreshment I believe) and some are #N/A (for the slower refreshment). All the data is refreshed every second, except upon opening the file where successfully exported values are refreshed instantly and #N/A values need few seconds (2-3s) to be refreshed.

Recorder macro uses ActiveWorkbook.UpdateLink as follows (the code below is for the first row, but the same logic is used and for the rest of the rows):

Sub PriceUpdate()
ActiveWorkbook.UpdateLinks = xlUpdateLinksAlways

ActiveWorkbook.UpdateLink Name:= _
    "vegadde|VEGA!897789,148,1@""1,\""12,0,0\"""",1", Type:=xlOLELinks
ActiveWorkbook.UpdateLink Name:= _
    "vegadde|VEGA!897789,148,1@""1,\""12,0,0\"""",3", Type:=xlOLELinks
ActiveWorkbook.UpdateLink Name:= _
    "vegadde|VEGA!897789,148,1@""1,\""12,0,0\"""",4", Type:=xlOLELinks

    ...

End Sub

Then I use vbscript to call the macro and export data in CSV comma delimited file with timestamp name:

Option Explicit

Dim objExcel, objBook, objSheet

Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open ("d:\exptest\exptest.xlsm", 0, False)
'Set objSheet = objBook.Worksheets.Item(1)
objExcel.DisplayAlerts = False

objExcel.Run "PriceUpdate"
WScript.Sleep 5000 ' Delay in order to update links 
objBook.SaveAs "d:\exptest\" & Year(Now) & "." & Month(Now) & "." & Day(Now) & "_" & Hour(Now) & "-" & Minute(Now) & ".txt",6

objBook.Close False

objExcel.DisplayAlerts = True

objExcel.Quit

'Set objSheet = Nothing
Set objBook = Nothing
Set objExcel = Nothing

And what I get is the following uotput:

Inst,Price,Datetime
USD,1.1015,7/22/2016 12:48
GBP,#N/A,#N/A
CHF,#N/A,#N/A
SEK,9.4962,7/22/2016 12:48
NOK,#N/A,#N/A
JPY,#N/A,#N/A
DKK,#N/A,#N/A
CAD,#N/A,#N/A
AUD,1.47395,7/22/2016 12:48
RUB,71.0082,7/22/2016 12:48

I'm struggling to learn Excel logic behind different updates in order to solve my problem and to get desired solution, but with no success.

And this is only a test file with 22 links. The real one has 482 links that need to be updated.

0

Marjan. Not sure if this is still a problem for you, but I ran into a similar problem with refreshing RTD data in a closed Excel workbook and seem to have found a solution. As you mentioned, when you open the workbook, the data refreshes automatically (with some of the more complex data points taking 2-3 sec). The problem is, you don't want to have to constantly manually open a workbook to refresh data. And I've found that the data fails to refresh with methods like RTD.RefreshData called to a closed workbook.

So to fix this, I created a vba module that simply opens the workbook, refreshes it, pauses for a second (you may need 2-3 more seconds for all data to update) and then saves and closes the workbook. This method is called recursively. Essentially this does the work of constantly opening and closing the workbook, while delaying briefly so that the RTD data can refresh. So far, this is able to provide refreshed RTD data successfully (although there is an inherent slight delay of a few seconds). Here's the basis of my code:

Sub refreshXLS()
    Path = "workbook.xlsm"  'the workbook path you want to refresh

    Workbooks.Open Path
    ActiveWorkbook.RefreshAll
    Application.OnTime Now() + TimeValue("00:00:01"), "closeActive" 
End Sub


Sub closeActive()
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    refreshXLS
End Sub

Hope this helps if you still wish to solve this problem!

nick
  • 18
  • 4