excel - To import data from file to existing workbook using Vlookup -
i have main workbook , sub workbook macro commands open
1) main workbook = database
worksheet in main workbook = customer database
sub workbook = orders
these 2 books open macro run.
1) need import data range c:f
orders
customer database b:e
if cell b
orders
contains new member
2) worksheet in database contains data. hence, want add on existing sheet in database
please help.
dim wssource worksheet dim wbsource workbook dim wstarget worksheet dim wbtarget workbook dim findrange range dim lastline integer dim file string file = "orders.xlsx" set wbsource = thisworkbook set wssource = wbsource.sheets(1) set wbtarget = workbooks.open(file) set wstarget = wbtarget.sheets(1) wstarget.activate set findrange = wstarget.range("c2:f389") findrange.replace what:="xxx-string",replacement:=wssource.range("b2:e2").value, lookat:=xlpart, searchorder:=xlbyrows, matchcase:=false lastline = wbsource.sheets(1).range("x65536").end(xlup).row dim d integer dim j integer d = 3 j = 2 b = 1 lastline if wstarget.range("b" & j) = "new member" d = d + 1 wssource.range("b" & j).value = wstarget.range("c" & d).value wssource.range("c" & j).value = wstarget.range("d" & d).value wssource.range("d" & j).value = wstarget.range("e" & d).value wssource.range("e" & j).value = wstarget.range("f" & d).value end if j = j + 1 next b
this answer based on items 1 , 2 enumerated above.
i'm not sure want accomplish .replace
method remove it.
sub consolidate() dim wssource worksheet dim wbsource workbook dim wstarget worksheet dim wbtarget workbook dim findrange range dim lastline integer dim file variant '~~> declared variant set wbsource = thisworkbook set wssource = wbsource.sheets(1) '~~>use index if have 1 sheet file = application.getopenfilename("excel files, *.xlsx") '~~> allows select file load if file <> false set wbtarget = workbooks.open(file) set wstarget = wbtarget.sheets(1) wstarget lastline = .range("b" & .rows.count).end(xlup).row set findrange = .range("b1:f" & lastline) findrange .autofilter 1, "new member" .offset(1,1).resize(.rows.count-1, .columns.count-1).specialcells(xlcelltypevisible).copy _ wssource.range("b" & wssource.rows.count).end(xlup).offset(1,0) end end else msgbox "no file selected. exiting now." : exit sub end if wbtarget.close false wbsource.save end sub
no need loop through whole range.
used .autofilter
method instead.
hope close want.
Comments
Post a Comment