refreshing a querytable connection on opening a worksheet VBA

star1984

New Member
Joined
May 12, 2011
Messages
19
hi,
I have the following code which is updating the connection held for all querytables in my worksheet.
This works fine however I also want to automatically get the querytabke to refresh - ie in Excel the user goes to Data>Connections> and presses the refresh button. Is there a way to access this refresh method through VBA code. I am thinking something along the lines of
Worksheet.QueryTables.Item(itemcount).Connection.Refresh but this doesn't work

For Each Worksheet In ThisWorkbook.Worksheets
itemcount = 0
For Each Item In Worksheet.QueryTables
itemcount = itemcount + 1
Worksheet.QueryTables.Item(itemcount).Connection = SQLConn
Next
Next

Any suggestions?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
By recording a macro and viewing the autogenerated code I have found that line I want is

ActiveWorkbook.Connections("Connection").Refresh

However - "Connection" is hardcoded - if I have multiple connections on one worksheet I wont want to hardcode this I'll want the connectionname. Is there a way to read the name property of a connection?
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

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