excel vba - Run-time error when deleting a named range -
i don't understand why keep getting "run-time error '1004': application-defined or object-defined error" message when trying delete named range.
here's code used import data .csv file , name range "history"
with activesheet.querytables.add(connection:= _ "text;c:\users\<user name>\downloads\history.csv", destination:=range(destcell)) .name = "history" .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 = xldelimited .textfiletextqualifier = xltextqualifierdoublequote .textfileconsecutivedelimiter = false .textfiletabdelimiter = false .textfilesemicolondelimiter = false .textfilecommadelimiter = true .textfilespacedelimiter = false .textfilecolumndatatypes = array(3, 1, 2, 1, 1) .textfiletrailingminusnumbers = true .refresh backgroundquery:=false end
here's code used delete "history" named range. note line before functions fine find named range. doesn't won't delete name.
application.goto reference:="history" activeworkbook.names("history").delete
answer: problem workbook using worksheet name part of name attribute of named range. using history!history name.
method of troubleshooting: used following code had been posted similar question @ http://www.ozgrid.com/forum/showthread.php?t=49079&page=2
dim namerng name dim varval variant on error resume next each namerng in activeworkbook.names varval = range(namerng.name).value namerng.delete next
the locals window in vba editor revealed namerng.name variable string "history!history".
revised code: removed application.goto reference:="history" line since non-functional line of code (similar select action) , left code delete name of imported range:
activeworkbook.names("history!history").delete
platform: using excel 2013 on windows 7 professional
Comments
Post a Comment