Rastaman
Board Regular
- Joined
- Feb 18, 2006
- Messages
- 236
Im hoping someone can help with a problem dealing with data connections. Im trying to create a tool that will refresh an existing web data connection on the active sheet, prompting the user to input a new URL. This is a generic utility, intended to work on any existing data connection. My plan is as follows:
Find any data connections on the active sheet
Verify there is only one connection
Prompt user for a new URL
Refresh the query
I first want to check to see if a data connection exists. I could use something like the code below, but I havent figured out how to tell if the connection is on the active sheet. Also, this connection name does not work in the Querytables(TestConn).Refresh command. The TestConn name with this code matches the connections name, but the Querytables Refresh command requires the data range properties name. These two names do not match unless someone had previously renamed them to match, which I canÂt count on in my generic macro.
So instead I found that I can loop through each named range in the active sheet to find the data connection. In my testing this works well since I rarely have a named range with a scope of worksheet besides data connections. I just have to parse the resulting name string to remove the sheet name.
Since this isnt 100% certain to find the right data connection, I am trying to loop through all data connections and validate that the named range that I found is an actual data connection. But I have the same problem that I cant compare the named range name with the data connection name. Is there a better approach to this?
Thanks
Find any data connections on the active sheet
Verify there is only one connection
Prompt user for a new URL
Refresh the query
I first want to check to see if a data connection exists. I could use something like the code below, but I havent figured out how to tell if the connection is on the active sheet. Also, this connection name does not work in the Querytables(TestConn).Refresh command. The TestConn name with this code matches the connections name, but the Querytables Refresh command requires the data range properties name. These two names do not match unless someone had previously renamed them to match, which I canÂt count on in my generic macro.
Code:
For Each DataConn In ActiveWorkbook.Connections
TestConn = DataConn.Name
If ConnName = TestConn Then FoundMatch = "Yes"
Next DataConn
So instead I found that I can loop through each named range in the active sheet to find the data connection. In my testing this works well since I rarely have a named range with a scope of worksheet besides data connections. I just have to parse the resulting name string to remove the sheet name.
Code:
For Each nm In ActiveSheet.Names
ConnName = nm.Name
NameCount = NameCount + 1
Next nm
Since this isnt 100% certain to find the right data connection, I am trying to loop through all data connections and validate that the named range that I found is an actual data connection. But I have the same problem that I cant compare the named range name with the data connection name. Is there a better approach to this?
Thanks