Forum Discussion
Excel VBA Macro inadvertantly renaming Power Query queries
I've been trying to troubleshoot this for awhile now but am coming up blank. For the past couple of months a VBA subroutine I have has been renaming my M queries after the first run-through to QUERYNAME (2). Subsequently the second time through the loop (with different parameters specified) the queries fail to refresh because they no longer have the same names. The closest thing I could find to this online is this article from back in 2015.
Here are some cavaets to what I'm doing...
- All queries are refreshing in Background Mode of False because I need to refresh them in order.
- I've tried them in "Load to Table" only, "Only Create Connection" and "Load to Data Model" and various variations thereof. Some must be loaded to worksheets as I'm copying over the data into new workbooks as part of the output process.
- It's not at all clear when stepping through the VBA code when the queries get renamed. I've even tried some VBA watches on "Break when True" but they don't seem to fire in this regard.
- As I said this was working several months ago but so many changes have happened since then (most of them good) that I don't know which change broke it.
- In order to properly troubleshoot this and improve the performance, I've separated the former "Get and transform all the data" steps and the "Generate all the workbooks" steps into separate processes.
- Despite #4 above, there are still there are still several (a dozen or so) queries in the "Generate all the workbooks" workbook some of which are interconnected and I think I've already paired it down to a bare minimum. (The previous "Do all the stuff" workbook had some 40+ queries, many of which were custom M functions, in it.)
Any thoughts?
7 Replies
- AnonymousNot applicable
Hi pelowski ,
If such error occurs in Excel or Power Query?
According to the error message, it is caused by renaming queries, so that we could not access to the data connection any more.You may click on the "Debug" button to see more details.
Best Regards,
Eyelyn Qin- pelowski
Helper III
What? I've been debugging for days. Of course I've clicked "Debug" in the first prompt shown and the indication is that the query that I want to refresh in the VBA subroutine has been renamed and is therefore not available via the name in which I'm calling it.
The PQ queries are getting renamed somehow in the VBA execution and I'm asking if anyone else has experienced this because figuring out where they are getting renamed in the VBA has not been successful thus far even with stepping through the code and using Debug.Asserts (https://youtu.be/CT7XkPXKVFw?t=368) everywhere to try to figure it out.
- lbendlin
Super User
this should help
excel - How to automate a power query in VBA? - Stack Overflow
Look for "ListObjects" and "Connections" in your VBA code.
- pelowski
Helper III
This is for anyone that might care about this solution. After a lot of debugging and refactoring of this code I think I'm on the right path. I'm changing any time I copy a sheet that is based on query results from VBA code that looks like this...
ForReview.Copy After:=wbCreated.Sheets(2)
To looking something like this instead...
wbCreated.Sheets.Add.Name = "For Electronic Review"
Set rngToCopy = wbMain.Sheets("For Review").Range("A1").CurrentRegion
Set rngToPaste = wbCreated.Sheets("For Electronic Review").Range(rngToCopy.Address)
rngToCopy.Copy
rngToPaste.PasteSpecial Paste:=xlPasteValues
rngToPaste.PasteSpecial Paste:=xlPasteFormats
Call FormatForReview(wbCreated.Sheets("For Electronic Review"))(There are several output worksheets in various output workbooks that have very specific formatting that I need to recreate in areas.)
The downsides of this approach are...
1. There are more steps to the copy and despite everything being already formatted in the source worksheet not all of it (like images that have been added) get carried over like before so sometimes I need additional formatting steps after the PasteValues/PasteFormats.
2. I have to recreate any table declarations and reformat them where necessary because the PasteFormats doesn't get me that either.
The upsides are...
1. My query names aren't getting clobbered during the workbook creation
2. I don't have to loop through the Destination workbook afterwards and remove queries and named ranges that inadvertently got copied over during the previous method of worksheet copy.
Overall, I still don't have it done, but I'm at least closer to solving this problem.
- pelowski
Helper III
I thought this was on the right path the solving the issue, but unfortunately I'm running into other issues now including queries not existing the next time I try to do a refresh. I'm really loathing VBA and query refreshing right now.