Application.Run timing problem

matteobusetti

New Member
Joined
Aug 23, 2015
Messages
6
Hi all,

I am writing a macro that has to open another workbook (wb2), run a macro in wb2, copy some data and paste it to the first workbook.

However, given it takes some time for Application.Run to actually populate the excel cells in wb2, it seems like the macro i am writing selects the range to copy and copies it before Application.Run has populated the cells and therefore copy empty cells.

Is there a way to tell VBA to wait until Application.Run has populated the entire wb2 before proceeding with selecting the range to copy?

Many thanks
Matteo


Code:
Sub Import_Data1()

' Import_Data1 Macro


    'set current workbook
    Set wb1 = ActiveWorkbook
    Set ws1 = wb1.Worksheets("Performance")
    
    'open IB API workbook
    Set wb2 = Workbooks.Open("Workbook2")
    Set ws2 = wb2.Worksheets("Portfolio")
    
    'Selects tab
    Sheets("Portfolio").Select
    
    'enter username
    Range("B5").Select
    ActiveCell.FormulaR1C1 = "username"
    
    'run macro in IB API file to get portfolio data
    Application.Run "TwsDde.xls!Sheet15.subscribeToPorts"
    
    'select first 20 rows and copy
    Range("A8:B28").Select
    Selection.Copy
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Untested. Does this help after you adjust the delay time for your macro to populate values?
Code:
'run macro in IB API file to get portfolio data
    Application.Run "TwsDde.xls!Sheet15.subscribeToPorts"
    T = Now
    Do While TW <= T + TimeSerial(0, 0, 2)  'Adjust delay time here
        TW = Now
        DoEvents
    Loop
    'select first 20 rows and copy
    Range("A8:B28").Select
    Selection.Copy
 
Upvote 0
Untested. Does this help after you adjust the delay time for your macro to populate values?
Code:
'run macro in IB API file to get portfolio data
    Application.Run "TwsDde.xls!Sheet15.subscribeToPorts"
    T = Now
    Do While TW <= T + TimeSerial(0, 0, 2)  'Adjust delay time here
        TW = Now
        DoEvents
    Loop
    'select first 20 rows and copy
    Range("A8:B28").Select
    Selection.Copy


Work great! thanks!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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