Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 2t+59EUK027p9ahknOAEcJ
Power Apps - Power Query
Unanswered

Use variable to identify which query to refresh? (Excel, VBA, PowerQuery)

Like (1) ShareShare
ReportReport
Posted on 8 Jul 2024 23:18:53 by

I'm rebuilding some old reports that have gotten slow, and am using queries rather than multiple connections to the same server and database this time.

 

In the original and new versions, each report has a bunch of tabs, and each tab has two buttons that run macros: one to refresh the data in that specific tab, and one to clear the data.  This time I'd love to create only one piece of VBA code to refresh the query for the active worksheet, and pass a variable into it that identifies which query that should be.  Doing that to clear the data is simple, but I haven't figured out a way to refresh.

 

For each query I've used Get Data > From Database > SQL, pasted in my query, hit Transform, and given it a specific name.  Of course, hitting Close & Load the first time creates the tab for me and gives it the name of the query.  It's simple enough to write a worksheet's name into a variable (I named mine the very original "sheetNm",) but how can I pass that variable into the refresh command?

 

This is an example what I'm working with:

ActiveWorkbook.Connections("Query - Fourth_Tab-L").Refresh

 

Naturally, I can't replace the name of the query with the name of the variable, that just triggers a subscript out of range error.  Have any of you ever done this successfully?

 

Thanks for your time!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Power Query

#1
mmbr1606 Profile Picture

mmbr1606 9 Super User 2025 Season 1

#2
SD-13050734-0 Profile Picture

SD-13050734-0 6

#3
Jon Unzueta Profile Picture

Jon Unzueta 2

Overall leaderboard

Featured topics

Loading started