|
Question : Excel Database Query Definition Disappears
|
|
I have several Excel 2003 worksheets that are linked to an external data source using sql queries. I am experiancing intermitant problems where the query definitions are randomly deleted ie I can no longer refresh the data and must recreate the query from scratch, which is driving me nuts to say the least.
I have read a few of the thread on this site including the one titled "Excel database queries disappear" http:/Q_20531254.html that talks about defined name ranges having spaces in them. I tried this possible fix, and yes the name ranges did have spaces in them but removing them did not fix my problem.
Has anyone come across this problem or any other possible solutions / causes in the 2 years since this other thread was posted ?
Thanks
Karen
<>
|
|
Answer : Excel Database Query Definition Disappears
|
|
Hi, The following procedure loops through all worhseets and all queries, and it re-sets their range names based on the query name. Your could run it manually, or have it run automatically when the book opens and after each query refresh. It assumes that the non-working named range with extra spaces is still understood as a range by Excel.
It re-sets query range names based on the query names; that is, it requires query names to be simple: - it takes the name of the query , - replaces any space with a _ - and reset that name (which should be a valid name for a named range (ie: ?!% won't work in the name of the query). Eg1: query named 'MyQuery' (in menu Data > Get Ext. Data > Data Range Properties), it finds the name 'MyQuery' and resets it to the range occupied by the query. Eg2: query named 'My Query' (with a Space); its looks for the named range 'My_Query' (replacing the space with undescore) and resets it.
The sub logs each 'reset' to the immediate window in the VBA editor (to check what works , what doesn't). The messages are: - Finds a query but cannot match its named range: "Could not match name for sheet , query: " - Finds a query and its named, but cannot reset its name: "Could not reset name for sheet , query: " - Finds a query, its, name too, and reset its name successfuly: "Reset name for sheet , query: "
'--------------------------------------------------------- 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 '---------------------------------------------------------------
I hope this helps,
Regards, Sebastien
|
|
|
|