excel vba - VBA to prompt for DAT with no delimitation, add new sheet, and import the DAT -
the code prompts user select excel file , 5 different dat files. excel file loaded onto sheet, , new sheets supposed added each of dat files imported to. excel file loads correctly, program errors out @ first dat file import attempt.
the error: "run-time error '1004': application-defined or object-defined error".
this error occurs:
activesheet.querytables.add(connection:= _ difn, destination _
this rest of code:
' prompt user files cafn = application.getopenfilename("excel files (*.xlsx), *.xlsx") difn = application.getopenfilename("esdi dat file (*.dat), *.dat") fofn = application.getopenfilename("esfo dat file (*.dat), *.dat") fsfn = application.getopenfilename("esfs dat file (*.dat), *.dat") ipfn = application.getopenfilename("esip dat file (*.dat), *.dat") ppfn = application.getopenfilename("espp dat file (*.dat), *.dat") ' load combined dim x workbook dim y workbook '## open both workbooks first: set y = activeworkbook set x = workbooks.open(cafn) 'now, transfer values x y: x.sheets("sheet1").usedrange 'now, paste y worksheet: y.sheets("start").range("a1").resize( _ .rows.count, .columns.count) = .value end 'close x: x.close y.sheets("start").name = "combined all" ' load dat files activeworkbook.worksheets.add.name = "esdi" activesheet.querytables.add(connection:= _ difn, destination _ :=range("$a$1")) .name = "esdi" .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 = 1 .textfileparsetype = xlfixedwidth .textfiletextqualifier = xltextqualifierdoublequote .textfileconsecutivedelimiter = false .textfiletabdelimiter = true .textfilesemicolondelimiter = false .textfilecommadelimiter = false .textfilespacedelimiter = false .textfilecolumndatatypes = array(9, 1, 1, 9, 1, 9, 9, 1, 9, 9, 9) .textfilefixedcolumnwidths = array(3, 7, 7, 2, 10, 8, 3, 8, 40, 2) .textfiletrailingminusnumbers = true .refresh backgroundquery:=false end
this code worked me (thank yowe3k , scott holtzman pointing me in right direction!):
' prompt user files dim cafn string dim difn string dim fofn string dim fsfn string dim ipfn string dim ppfn string cafn = application.getopenfilename("excel files (*.xlsx), *.xlsx") difn = application.getopenfilename("esdi dat file (*.dat), *.dat") fofn = application.getopenfilename("esfo dat file (*.dat), *.dat") fsfn = application.getopenfilename("esfs dat file (*.dat), *.dat") ipfn = application.getopenfilename("esip dat file (*.dat), *.dat") ppfn = application.getopenfilename("espp dat file (*.dat), *.dat") ' load combined dim x workbook dim y workbook '## open both workbooks first: set y = activeworkbook set x = workbooks.open(cafn) 'now, transfer values x y: x.sheets("sheet1").usedrange 'now, paste y worksheet: y.sheets("start").range("a1").resize( _ .rows.count, .columns.count) = .value end 'close x: x.close y.sheets("start").name = "combined all" ' load dat files activeworkbook.worksheets.add.name = "esdi" msgbox ">>>" & "text;" & difn & "<<<" sheets("esdi").querytables.add(connection:="text;" & difn, destination:=range("$a$1")) .name = "esdi" .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 = 1 .textfileparsetype = xlfixedwidth .textfiletextqualifier = xltextqualifierdoublequote .textfileconsecutivedelimiter = false .textfiletabdelimiter = true .textfilesemicolondelimiter = false .textfilecommadelimiter = false .textfilespacedelimiter = false .textfilecolumndatatypes = array(9, 1, 1, 9, 1, 9, 9, 1, 9, 9, 9) .textfilefixedcolumnwidths = array(3, 7, 7, 2, 10, 8, 3, 8, 40, 2) .textfiletrailingminusnumbers = true .refresh end
Comments
Post a Comment