Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel query creates named range

Gary,
I agree with Steve's approach. I encounter the same problem when using
MS Query or with a SQL query.

Here are two procedures that I call when I need to clear out any such range
names.

For use after MS Queries:

Public Sub DeleteQueryDefinedNames()
Dim n As Name
For Each n In ActiveWorkbook.Names
If n.Visible = False And InStr(1, n.Name, "QUERY", _
vbTextCompare) 0 And InStr(1, n.Name, _
"Query_from", vbTextCompare) = 0 Then
n.Delete
End If
Next
End Sub

You can replace the "Query" and "Query_from" strings with your own specific
"ClientTracking_" strings to get it to work.


For use after SQL queries:

Public Sub Delete_External_Names()
Dim nName As Name
On Error Resume Next
' First worksheet with names to delete
For Each nName In Application.Names
If nName.Name Like "SpecCalc!ExternalData*" Then
Application.Names(nName.Name).Delete
End If
Next nName
' Second worksheet with names to delete
For Each nName In Application.Names
If nName.Name Like "Metered_Ready!ExternalData*" Then
Application.Names(nName.Name).Delete
End If
Next nName
On Error GoTo 0
End Sub

This looks on specific worksheets for named ranges and deletes names that
start with "ExternalData". I discovered that I was accumulating both
workbook level and worksheet level named ranges after my queries. The sheet
names in this example were "SpecCalc" and "Metered_Ready".
You can replace them with your specific sheet names and the "ExternalData"
string can be replaced with your "ClientTracking_" string to get it to work
for you. If you have more than two worksheets, then additional For..Next
loops can be added.

I call on one or both of these procedures after running a query I have used.
It seems to keep the range names from accumulating in my workbooks.

HTH
Ed Gravley


"Gary West" wrote in message
8.16...
I created a query and then recorded the macro needed to save the code to
run it. I just discovered that every time it runs it inserts a named
range. A number getsincremented and added to the end of the range name
and somewhere Excel is keeping track of the last name even though I
deleted them from the sheet.

The first time I ran the query it created a range named ClientTracking_
1. The 2nd time I got ClientTracking_2. I stumbled across this by the
time it was up to _32. I deleted all the ranges, ran the query again and
the new one got called _33!

What I would like is to reset the named ranges to zero AND either have a
way to prevent this query from creating new ranges or, if that's not
possible, learn how to delete them programmatically.


Thanks. - gary

With ActiveSheet.QueryTables.Add(Connection:= _
"FINDER;C:\Documents and Settings\querypath\query.dqy" _
, Destination:=Cells(Rows.Count, "B").End(xlUp).Offset(1, 0))
.Name = "ClientTracking"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Excel query creates named range

Ed,

Thanks!

You might also want to just cycle through all the worksheets

Dim x as Long
Application.ScreenUpdating = False
For x = 1 to Activeworkbook.Worksheets.Count
Sheets(x).Select
*call code to delete names*
Next
Application.ScreenUpdating = True

The deletion is usally pretty fast, so speed shouldn't be a problem.

steve

"Ed Gravley" wrote in message
...
Gary,
I agree with Steve's approach. I encounter the same problem when

using
MS Query or with a SQL query.

Here are two procedures that I call when I need to clear out any such

range
names.

For use after MS Queries:

Public Sub DeleteQueryDefinedNames()
Dim n As Name
For Each n In ActiveWorkbook.Names
If n.Visible = False And InStr(1, n.Name, "QUERY", _
vbTextCompare) 0 And InStr(1, n.Name, _
"Query_from", vbTextCompare) = 0 Then
n.Delete
End If
Next
End Sub

You can replace the "Query" and "Query_from" strings with your own

specific
"ClientTracking_" strings to get it to work.


For use after SQL queries:

Public Sub Delete_External_Names()
Dim nName As Name
On Error Resume Next
' First worksheet with names to delete
For Each nName In Application.Names
If nName.Name Like "SpecCalc!ExternalData*" Then
Application.Names(nName.Name).Delete
End If
Next nName
' Second worksheet with names to delete
For Each nName In Application.Names
If nName.Name Like "Metered_Ready!ExternalData*" Then
Application.Names(nName.Name).Delete
End If
Next nName
On Error GoTo 0
End Sub

This looks on specific worksheets for named ranges and deletes names that
start with "ExternalData". I discovered that I was accumulating both
workbook level and worksheet level named ranges after my queries. The

sheet
names in this example were "SpecCalc" and "Metered_Ready".
You can replace them with your specific sheet names and the "ExternalData"
string can be replaced with your "ClientTracking_" string to get it to

work
for you. If you have more than two worksheets, then additional For..Next
loops can be added.

I call on one or both of these procedures after running a query I have

used.
It seems to keep the range names from accumulating in my workbooks.

HTH
Ed Gravley


"Gary West" wrote in message
8.16...
I created a query and then recorded the macro needed to save the code to
run it. I just discovered that every time it runs it inserts a named
range. A number getsincremented and added to the end of the range name
and somewhere Excel is keeping track of the last name even though I
deleted them from the sheet.

The first time I ran the query it created a range named ClientTracking_
1. The 2nd time I got ClientTracking_2. I stumbled across this by the
time it was up to _32. I deleted all the ranges, ran the query again and
the new one got called _33!

What I would like is to reset the named ranges to zero AND either have a
way to prevent this query from creating new ranges or, if that's not
possible, learn how to delete them programmatically.


Thanks. - gary

With ActiveSheet.QueryTables.Add(Connection:= _
"FINDER;C:\Documents and Settings\querypath\query.dqy" _
, Destination:=Cells(Rows.Count, "B").End(xlUp).Offset(1, 0))
.Name = "ClientTracking"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel query and link named range Jessica Links and Linking in Excel 0 July 6th 09 09:10 PM
MS Query Refresh Based on Dynamic Named Range bwilk77 Excel Discussion (Misc queries) 0 May 26th 09 02:17 PM
How Do I Query a Named Range JeffP-> Excel Worksheet Functions 2 February 3rd 09 11:11 PM
Query Named Range Return Single Column Value JeffP-> Excel Worksheet Functions 5 November 29th 07 10:30 PM
Excel query creates named range Dick Kusleika Excel Programming 2 August 18th 03 06:49 PM


All times are GMT +1. The time now is 05:43 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"