Macro to refresh n disable query

vbacoder12

New Member
Joined
Sep 4, 2024
Messages
24
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi I am not too familiar with Marcos. But is there a macro that can be written to refresh a single query then disable refreshing. If so can you please show me the code which would do this.i do not know where to begin
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You can "sort of disable" the query refreshing by uncheking the "Refresh this connection on Refresh All" found in the Query Properties. If you protect the worksheet ( with or without a password ) your query is about as disabled as it gets without deleting the actual query.

When you're done this you can use simple macro to unprotect the sheet, refresh the query and protect the sheet again:
VBA Code:
Dim PW As String

PW = "123"          ' Set the password

    ActiveSheet.Unprotect (PW)  ' Unprotect active sheet using the password
    ActiveWorkbook.Connections("Query - YourQueryName").Refresh      ' Refresh the Query
    ActiveSheet.Protect (PW)    'Protect active sheet using the password

If you don't want to use a password to protect / unprotect your worksheet remove the "(PW)" from the code.

Replace the ActiveSheet references with actual sheet references to make the macro more error proof. As it is the macro will crash if you're running the macro from a wrong sheet.

If you don't know what is the name of your query you can record a macro when you refresh the macro manually. You can't refresh the queries on protected sheets so do this before protecting the sheet or when the sheet is unprotected.
 
Upvote 0
You can "sort of disable" the query refreshing by uncheking the "Refresh this connection on Refresh All" found in the Query Properties. If you protect the worksheet ( with or without a password ) your query is about as disabled as it gets without deleting the actual query.

When you're done this you can use simple macro to unprotect the sheet, refresh the query and protect the sheet again:
VBA Code:
Dim PW As String

PW = "123"          ' Set the password

    ActiveSheet.Unprotect (PW)  ' Unprotect active sheet using the password
    ActiveWorkbook.Connections("Query - YourQueryName").Refresh      ' Refresh the Query
    ActiveSheet.Protect (PW)    'Protect active sheet using the password

If you don't want to use a password to protect / unprotect your worksheet remove the "(PW)" from the code.

Replace the ActiveSheet references with actual sheet references to make the macro more error proof. As it is the macro will crash if you're running the macro from a wrong sheet.

If you don't know what is the name of your query you can record a macro when you refresh the macro manually. You can't refresh the queries on protected sheets so do this before protecting the sheet or when the sheet is unprotected.
Thanks. I used the delQueries sub.which removed the q
uery connections from the workbook.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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