Excel VBA: How to compare if values appear in both columns and copy its adjacent cell values to another column? -
i trying search 2 set of data (from jul , aug) , compare results
- countries appear in jul , aug
- countries appear in jul , not in aug
- calculate percentage increase or decrease on count value country (appearing in jul , aug)
below excel screenshot i'm referring to
- take value 'america' in cell a2 , search in column e
- if found, copy values in cell a1, b1 , c1 column h1, i1 & j1
question
- now want copy value in column f column k (if same country appear in aug , jul).
- for example cananda search , code logic can copy values in same row not in cell f2 (cos different row)? ideas share how it?
below vba codes
sub compare() dim report worksheet, lastrow long, fval range, c range, long set report = sheets(1) 'edit sheet name lastrow = report.cells(rows.count, 1).end(xlup).row 'for each c in report.range("a2:a10") 'assumes header row = 2 lastrow 'set fval = report.range("e2:e" & lastrow).find(c.value, lookin:=xlvalues, lookat:=xlwhole) set fval = report.range("e2:e" & lastrow).find(report.cells(i, 1).value, lookin:=xlvalues, lookat:=xlwhole) if not fval nothing 'c.interior.colorindex = 6 report.cells(i, 1).interior.colorindex = 6 report.cells(i, 8) = report.cells(i, 1) report.cells(i, 9) = report.cells(i, 2) report.cells(i, 10) = report.cells(i, 3) else report.cells(i, 13) = report.cells(i, 1) report.cells(i, 14) = report.cells(i, 2) end if next end sub
i think want. have shortened code little using resize. need refer found cell (in case 1 cell right understand it).
sub compare() dim report worksheet, lastrow long, fval range, c range, long set report = sheets(1) 'edit sheet name lastrow = report.cells(rows.count, 1).end(xlup).row = 2 lastrow set fval = report.range("e2:e" & lastrow).find(report.cells(i, 1).value, lookin:=xlvalues, lookat:=xlwhole) if not fval nothing report.cells(i, 1).interior.colorindex = 6 report.cells(i, 8).resize(, 3).value = report.cells(i, 1).resize(, 3).value report.cells(i, 11).value = fval.offset(, 1).value else report.cells(i, 13).resize(, 2).value = report.cells(i, 1).resize(, 2).value end if next end sub
Comments
Post a Comment