Hi All
I am using Excel 2003 SP3 and have had an ongoing issue with query tables being deleted after I refresh the data. Sometime ago a kind soul provided me with the following script that seems to work most of the time (but not always) assuming also that I have actually added this into my spreadsheet. Just wondering if anyone else has come up with a solution to this problem.
Thanks
Karen
Sub ReAssignQueryNames()
Dim wsh As Worksheet, qt As QueryTable, Name As Name, s As String
On Error Resume Next
Debug.Print "------Begin ReAssignQueryName-------------"
For Each wsh In ThisWorkbook.Worksheets
For Each qt In wsh.QueryTables
Set Name = wsh.Names(Strings.Replace(qt.Name, " ", "_"))
s = "sheet " & wsh.Name & ", query: " & qt.Name
If Err <> 0 Then
s = "Could not match name for " & s
Err.Clear
Else
wsh.Names.Add Name:=Name.Name, RefersTo:="='" & wsh.Name & "'!" & Name.RefersToRange.Address
If Err <> 0 Then
s = "Could not reset name for " & s
Err.Clear
Else
s = "Reset name for " & s
End If
End If
Debug.Print s
Next
Next
Debug.Print "------End ReAssignQueryName--------"
On Error GoTo 0
End Sub
Bookmarks