View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Query Tables naming not going to plan...

The Query names are named ranges you can look at by going to worksheet menu
Insert - Name - Define or from Files - properties - Contents


You can search the name ranges in a macro using the macro below

Sub getname()

Lookup = "PDEnglandWebQuery"

Set nms = ActiveWorkbook.Names
For r = 1 To nms.Count
If InStr(nms(r).Name, Lookup) 0 Then

End If
Next

End Sub

"George" wrote:

Hi everyone, I'm using Query Tables to grab some data from our corporate
intranet and it's all working very well apart from one minor problem...each
query gets "_x" added to the name, where x is a number (starting at one).

The code for the QueryTable is as follows...essentially I have code before
this to check if we already have a Query open on the sheet and it refreshes
it rather than creating a new one...

Set qt = ws.QueryTables.Add(connection, ws.Range("A1"))
With qt
.name = "PD" & tabName & "WebQuery"
.FieldNames = True
.PreserveFormatting = False
.RefreshStyle = xlOverwriteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.Refresh BackgroundQuery:=True
End With

"tabName" is just something to keep a track of which sheet we are
on...values can be anything you want (think countries).

When I create a QueryTable on each sheet in this way I get (for example):

PDEnglandWebQuery_1
PDAmericaWebQuery_1
...and so on

My refresh criteria is looking for the name set in the qt
("PDEnglandWebQuery") but obviously doesn't find it because of the _1.

Any suggestions on what I'm doing wrong?

Thanks
George