excel - Import/export named ranges and their values to and from workbook to .csv -


i have .csv 2 columns: cola has named ranges , colb has values.

now need import values .csv , assign them named ranges in workbook present in multiple sheets. need export same in same manner. i.e. workbook has named ranges , associated values.

is there way export in same format can use import them later?

for import modified code provided answer below still unsuccessful:

option explicit sub impdata() 'this import data csv xlsm  dim mycsv workbook dim filename string dim curfilename string dim myrange range dim mycell range dim x long dim y workbook  curfilename = thisworkbook.name filename = application.getopenfilename  set y = workbooks(curfilename)  application.screenupdating = false      set mycsv = workbooks.open(filename)     set myrange = mycsv.worksheets("import").range("b2:b7")       x = 1     each mycell in myrange.cells         range(thisworkbook.names(mycell.offset(, -1).value)).cells(x) = mycell.value     'method "range_of_object" global failed         x = x + 1     next mycell  mycsv.close savechanges:=false application.displayalerts = false   end sub 

this read values in csv - providing named range , values in csv same size , single column.

in example code csv has 2 different named ranges - a1:a3 hold 'namedrangea' , b1:b3 hold values, a4:a6 hold 'namedrangeb' , b4:b6 hold values. there 2 named ranges in excel workbook, both 3 rows 1 column.

sub readin()      dim mycsv workbook     dim myrange range     dim mycell range     dim x long      set mycsv = workbooks.open("c:\documents , settings\crladmin.adminnot\desktop\new folder\namestoranges.csv")     set myrange = mycsv.worksheets("namestoranges").range("a1:b6")      x = 1     each mycell in myrange.columns(2).cells         range(thisworkbook.names(mycell.offset(, -1).value)).cells(x) = mycell.value         x = x + 1     next mycell  end sub 

will point in right direction - have figure out reading out csv now.

edit: have rewritten code:

it ask location of csv, use first (and only) sheet in csv. have got rid of x variable realised wouldn't work if named ranges weren't. put next value in next empty cell in named range.

sub impdata()      dim mycsv workbook     dim mycsvpath string     dim myrange range     dim mycell range     dim mynextcell range     dim mynamedrange range      mycsvpath = getfile      if mycsvpath <> ""         set mycsv = workbooks.open(mycsvpath)         set myrange = mycsv.worksheets(1).range("b2:b7") 'ensure b2:b7 values are.          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  end sub  '--------------------------------------------------------------------------------------- ' procedure : getfile ' date      : 13/11/2013 ' purpose   : returns full file path of selected file ' use    : vfile = getfile() '           : vfile = getfile("s:\bartrup-cookd\customer services phone reports") '--------------------------------------------------------------------------------------- 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 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -