excel - how to change output location for each loop and run multiple loops -
i have code here loops through list of files; opening them, extracting data , moving main workbook. looking data abel in columns c , d put varo in f , g etc. problem see placement code inside loop each write on previous line instead of being in different column together!
sub source_data() dim r dim findvalues() string dim wrbk workbook dim workbook dim sht worksheet dim dim tmp dim counter dim c range dim firstaddress dim rng range redim findvalues(1 3) findvalues(1) = "abel" findvalues(2) = "varo" findvalues(3) = "tiger" counter = 0 r = range("a1").end(xldown).row set rng = range(cells(1, 1), cells(r, 1)) set = thisworkbook each tmp in rng workbooks.open tmp set wrbk = activeworkbook set sht = activesheet = 1 3 sht.range(cells(1, 1), range("a1").specialcells(xlcelltypelastcell)) set c = .find(findvalues(i), lookin:=xlvalues) if not c nothing firstaddress = c.offset(0, 2).value this.activate tmp.offset(0, 2).value = tmp.value tmp.offset(0, 3).value = firstaddress set c = .findnext(c) counter = counter + 1 loop while not c nothing , c.value = firstaddress end if end wrbk.activate next wrbk.close next tmp end sub
**edit:**i know can done adding multiplier of "i" offset value makes things bigger need if wish search 50 keywords
here answer, hope you, , always, if need improvement, tell me.
sub source_data() dim r dim findvalues() string dim wrbk workbook dim workbook dim sht worksheet dim dim tmp dim counter dim c range dim firstaddress dim rng range dim colnum 'the columns number var redim findvalues(1 3) findvalues(1) = "abel" findvalues(2) = "varo" findvalues(3) = "tiger" counter = 0 r = range("a1").end(xldown).row set rng = range(cells(1, 1), cells(r, 1)) set = thisworkbook each tmp in rng workbooks.open tmp set wrbk = activeworkbook set sht = activesheet = 1 3 sht.range(cells(1, 1), range("a1").specialcells(xlcelltypelastcell)) set c = .find(findvalues(i), lookin:=xlvalues) if not c nothing firstaddress = c.offset(0, 2).value this.activate select case 'test var (the value) case "abel" 'in case value (that string) equal to... colnum = 1 'set var, number of column want case "varo" 'in case value... colnum = 2 'set column... case "tiger" colnum = 3 case else 'in case var not match anyvalue take column number colnum = 20 'the garbage! end select tmp.offset(0, colnum).value = tmp.value 'put value in selected columns tmp.offset(0, colnum + 1).value = firstaddress 'and put value next column of 'selected column set c = .findnext(c) counter = counter + 1 loop while not c nothing , c.value = firstaddress end if end wrbk.activate next wrbk.close next tmp end sub
note: need set colnum
var values need, put there numbers of columns need store value of i
, next line put address of i
var
Comments
Post a Comment