Which version of Excel?
Regards,
Rory
I have several worksheets in a workbook that have dozens of queries. I would like to know how to make changes to the Query Table CommandText Property via Macro. For Instance, if a field name changes in the Data Source I would have to go through all 50+ queries to make the corresponding change. So, ultimately looking to do a sort of Find/Replace to the CommandText Property via Macro.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I really appreciate you working on this w/me. The Code snippet below contains what I have for code so far. It runs, however, it doesn't look like anything is getting changed. Here is a simplified version of one of my queries:
Command Type:
SQL
Command Text:
SELECT RTRIM(PG_NAME) AS PGNAME
FROM X
WHERE YEAR = 2009
added refresh step...code runs, queries refresh...still no changes. when i step through query i can see that the line "myQT.CommandText = Replace(myQT.CommandText, strOldField, strNewField)" is finding the commandtext and cycling through each query, but, for some reason the find/replace is not being performed.
replaced "myQT.CommandText = Replace(myQT.CommandText, strOldField, strNewField)" with "myQT.CommandText = Replace(myQT.CommandText, strOldField, strNewField, 1, -1, vbTextCompare)" and it looks like it worked...still some more testing to do, but, looks promising!!!! I will post full code and give u points a little later once i verify results etc.
Data Source is SQL Server. It could be either situation a) change of underlying field names in SQL table or b) extract different fields. Below is final code which I tested a few different ways and it works beautifully. This will be a big timesaver! Once again I appreciate your time and knowledge. Hope you had a great vaca!
Regards,
Swami
Business Accounts
Answer for Membership
by: spattewarPosted on 2009-09-02 at 14:19:19ID: 25245795
One way to do this would be to put the field names in a sheet in your workbook and build your commandtext using the field names in the sheet. So when there is a change you just change the field name in the sheet and that's it.
Yes, the first time it will be a change to all the 50+ queries though.
hope this helps.