Problem with VB that pulls data from files containing ODBC links

bkaehny

Board Regular
Joined
Jun 11, 2009
Messages
127
I've got four files from which I'm pulling data via some simple VB. Each of these files contains a table that's linked via ODBC to a View. These tables are all set to "refresh on open." When I open these files normally, the data does indeed refresh. However, when I open these files using VB, the data doesn't seem to refresh. Any ideas on what to do?

I thought the problem might be that the VB is going so fast, it's not giving the files enough time to refresh. So I tried inserting the following code after each Workbooks.Open command:

Application.Wait Now + TimeValue("00:00:20")

That doesn't seem to work, however. Any help you can give me would be greatly appreciated. Thank you.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
not sure if this makes a difference, but do you have events turned off when you run your code?
 
Upvote 0
Not that I'm aware of. This is how my macro begins:

Application.ScreenUpdating = False

Workbooks.Open Filename:= _
 
Upvote 0
It's the beginning part that's giving me trouble. "file A", "file B", "file C", and "file D" are the four files that contain linked table data. "master file" is the file that houses the macro, and it's the file that I'm copying and pasting data into.



Sub Macro()

Application.ScreenUpdating = False


Workbooks.Open Filename:= _
"file A.xlsx"
ActiveWorkbook.RefreshAll
Application.Wait Now + TimeValue("00:00:20")
ActiveWorkbook.Save
Workbooks.Open Filename:= _
"file B.xlsx"
ActiveWorkbook.RefreshAll
Application.Wait Now + TimeValue("00:00:20")
ActiveWorkbook.Save
Workbooks.Open Filename:= _
"file C.xlsx"
ActiveWorkbook.RefreshAll
Application.Wait Now + TimeValue("00:00:20")
ActiveWorkbook.Save
Workbooks.Open Filename:= _
"file D.xlsx"
ActiveWorkbook.RefreshAll
Application.Wait Now + TimeValue("00:00:20")
ActiveWorkbook.Save

Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Windows("master file.xlsm").Activate
Sheets("Invoice Lines").Select
Range("A2").Select
ActiveSheet.Paste
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR + 1).Select




Windows("file A.xlsx").Activate
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("master file.xlsm").Activate
ActiveSheet.Paste
LR2 = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR2 + 1).Select



Windows("file B.xlsx").Activate
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("master file.xlsm").Activate
ActiveSheet.Paste
LR3 = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR3 + 1).Select


Windows("file C.xlsx").Activate
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("master file.xlsm").Activate
ActiveSheet.Paste

Windows("file A.xlsx").Activate
ActiveWindow.Close
Windows("file B.xlsx").Activate
ActiveWindow.Close
Windows("file C.xlsx").Activate
ActiveWindow.Close
Windows("file D.xlsx").Activate
ActiveWindow.Close

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Rather than using the "refresh on open" functionality that you mentioned in your first post, would you be able to explicitly refresh the data in your macro?
 
Upvote 0
That seems to have worked. I also replaced the "ActiveWorkbook.RefreshAll" code with "Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False" to do the refresh inside the VB.

Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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