Skip to main content

Notifications

Community site session details

Community site session details

Session Id : N65/EA5FaMk7k4FVIKTdS5
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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,668 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,004 Most Valuable Professional

Leaderboard

Featured topics

Loading started