Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default New Named Range Created Each Time Data Imported into Excel via Macro

I recorded a macro that imports data into a sheet. I am finding that
each time I run this macro, a new named range is added to the list of
named ranges. Does anyone know why this is happening, and how I can
stop this action? This does not create a problem per se; it's just
that the list of names gets longer and longer.

Each new named range is for the same range, since I always import to
the same sheet/range. Each time the macro is run, the name has a
number at the end of the name that is incremented, e.g., receipt_20,
receipt_21, etc.

The recorded VBA looks like this:

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;K:\usrfiles\download\receipt.txt",
Destination:=Range("recpts!A2"))
.Name = "receipt"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 3
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2, 2, 3, 1, 1)
.TextFileFixedColumnWidths = Array(2, 1, 2, 10, 13)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

I tried commenting out the line with .Name in front, but it still
created a new named range.

Thanks,

Carroll Rinehart
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default New Named Range Created Each Time Data Imported into Excel via Macro

Create your querytable once (using the code you show). Then just refresh
it. (activeSheet.querytables(1).Refresh BackgroundQuery:=False)

otherwise, just before the beginning of your code shown do something like
for each nm in Thisworkbook.Names
if instr(1,nm.name,"receipt",vbTextCompare) then
nm.Delete
end if
Next
With ActiveSheet.QueryTables.Add(Connection:= _

--
Regards,
Tom Ogilvy


"Carroll Rinehart" wrote in message
om...
I recorded a macro that imports data into a sheet. I am finding that
each time I run this macro, a new named range is added to the list of
named ranges. Does anyone know why this is happening, and how I can
stop this action? This does not create a problem per se; it's just
that the list of names gets longer and longer.

Each new named range is for the same range, since I always import to
the same sheet/range. Each time the macro is run, the name has a
number at the end of the name that is incremented, e.g., receipt_20,
receipt_21, etc.

The recorded VBA looks like this:

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;K:\usrfiles\download\receipt.txt",
Destination:=Range("recpts!A2"))
.Name = "receipt"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 3
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2, 2, 3, 1, 1)
.TextFileFixedColumnWidths = Array(2, 1, 2, 10, 13)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

I tried commenting out the line with .Name in front, but it still
created a new named range.

Thanks,

Carroll Rinehart



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default New Named Range Created Each Time Data Imported into Excel via Macro

Tom,

I used the "nm.Delete: approach and it worked just fine. What I found
interesting is that somehow Excel knows what incremented number you're
on for the range name. So instead of having multiple named ranges, I
have just one, but its name is something like "receipt_62". I would
have thought that it would have started over with "receipt_1", but
that's really not important.

Thanks so much.

Carroll Rinehart

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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
Named Range DTS Import of Excel Data Harold I Excel Discussion (Misc queries) 0 September 28th 07 05:38 PM
Formatting time from imported data Wanna Learn Excel Discussion (Misc queries) 3 June 15th 07 07:52 PM
Named Range changes when data is imported Matt7102 Excel Discussion (Misc queries) 1 August 28th 06 10:03 PM
Macro run each time a new workbook created cmorton89 Excel Discussion (Misc queries) 0 March 8th 06 07:58 PM
Outlook wants an Excel named range to import contact data why? GDB026 Excel Discussion (Misc queries) 1 December 16th 05 04:59 PM


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

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

About Us

"It's about Microsoft Excel"