excel - Named Range error after importing values from a csv -
i have multiple csv workbooks 1 worksheet in it.
the worksheet has data in 2 columns:
cola colb namedrange1 valueofnamerange1 namedrange2 valueofnamerange2 namedrange3 valueofnamerange3 . . . . . . . . on total of 373 named ranges , values
now importing namedranges , values in worksheets has same namedranges , values can different well. want update values of namedranges importing csv.
the code below:
option explicit sub impdata() dim mycsv workbook dim mycsvpath string dim myrange range dim mycell range dim mynextcell range dim mynamedrange range dim ws worksheet dim finalrow long mycsvpath = getfile if mycsvpath <> "" set mycsv = workbooks.open(mycsvpath) application.screenupdating = false set ws = sheets(1) finalrow = ws.range("b90000").end(xlup).row set myrange = mycsv.worksheets(1).range("b1" & ":b" & finalrow) thisworkbook.activate each mycell in myrange.cells 'get reference named range. set mynamedrange = range(thisworkbook.names(mycell.offset(, -1).value)) 'find next empty cell in named range. set mynextcell = mynamedrange.cells(mynamedrange.cells.count).end(xlup).offset(1) 'if next empty cell above named range, set 'it first cell in range. if mynextcell.row < mynamedrange.cells(1).row set mynextcell = mynamedrange.cells(1) end if 'place value in range. mynextcell = mycell.value next mycell end if mycsv.close false application.screenupdating = true end sub '--------------------------------------------------------------------------------------- ' procedure : getfile ' date : 23/10/2015 ' purpose : returns full file path of selected file ' use : vfile = getfile() '--------------------------------------------------------------------------------------- function getfile(optional startfolder variant = -1) variant dim fle filedialog dim vitem variant set fle = application.filedialog(msofiledialogfilepicker) fle .title = "select file" .allowmultiselect = false .filters.add "comma separate values", "*.csv", 1 if startfolder = -1 .initialfilename = application.defaultfilepath else if right(startfolder, 1) <> "\" .initialfilename = startfolder & "\" else .initialfilename = startfolder end if end if if .show <> -1 goto nextcode vitem = .selecteditems(1) end nextcode: getfile = vitem set fle = nothing end function
now problem when import data csv file errors in values of namedranges in worksheet gets totally messed up.
this named manager box before importing csv correct.
this after importing data csv file. have filtered namedranges 1 errors.
you can refer this question related code. thank you.
Comments
Post a Comment