excel - VBA Testing two values, if one is different, copy -
i having fair amount of trouble code below:
sub testemail() dim long dim lastrow long dim worksheet dim b worksheet dim strtext dim objdata new msforms.dataobject set = workbooks("book2").worksheets(1) set b = workbooks("book1").worksheets(1) lastrow = a.cells(rows.count, "a").end(xlup).row = 2 lastrow if not iserror(application.match(a.cells(i, 7).value, b.columns(3), 0)) , iserror(application.match(a.cells(i, 4).value, b.columns(11), 0)) a.range("d" & i).copy objdata.getfromclipboard strtext = replace(objdata.gettext(), chr(10), "") b.range("k" & ).value = b.range("k" & ).value & " / " & strtext end if next end sub
i face 2 problems, 1 has me stumped , other due lack of knowledge:
the line after if supposed check if 2 values (numbers) in both workbooks match, , if 2 other values (text) don't match. if true, must copy value book2 , add cell in book1.
the problems are:
-the macro doesn't seem recognise when values match or not.
-in last line before "end if", don't know how tell excel copy text cell didn't match in second check.
i sorry if not clear enough, hard explain.
i'm hoping 1 of experts knows how make work.
thanks in advance
- you using
if not condition 1 , condition 2
, saying if doesn't match both conditions, run code. want make nested if statements however, 1if
, otherif not
- to copy missing
i
after "k"&:b.range("k" & i) = b.range("k" & i).value & " / " & strtext
- the address of cells inside range function, in case be:
//it cell of email first workbook tou copying, input column d
a.range("d" & i).copy
//add workbook b in column k value cell k#/value copied
b.range("k" & i) = b.range("k" & i).value & " / " & strtext
you can make this: b.range("k" & i) = b.range("k" & i).value & " / " & a.range("d" & i)
this way matching lines, if ids on same rows on both workbooks work. if aren't, have use nesting loops or .find function
edit:
- if understood it, code below might work if make changes application, because didn't have data test , columns, etc. try implement it.
lastrowa = a.cells(rows.count, "a").end(xlup).row lastrowb = b.cells(rows.count, "a").end(xlup).row = 2 lastrowa 'address of string lookforstring = a.worksheets(1).cells(i, 4) '4 column_index 'range on workbook a.worksheets(1).range("d1:d" & lastrowa) 'choose column 'function .find string on book set mail_a = .find(lookforstring, lookin:=xlvalues) if not mail_a nothing firstaddress = mail_a.address ' actions here 'range on workbook b b.worksheets(1).range("k1:k" & lastrowb) 'choose column 'function .find on workbook b set mail_b = .find(lookforstring, lookin:=xlvalues) if not mail_b nothing firstaddress = mail_b.address 'actions 'verify if 2 other values (text) don't match if not write_match_condition_here 'no need verify of equal because .find function used same reference 'i use .cells .row , .column show way , make dynamic b.cells(mail_b.adress.row, mail_b.adress.column) = b.cells(mail_b.adress.row, mail_b.adress.column).value & " / " & a.cells(mail_a.adress.row, mail_a.adress.column) 'choose columns end if set mail_b = .findnext(mail_b) loop while not mail_b nothing , mail_b.address <> firstaddress end if end set mail_a = .findnext(mail_a) loop while not mail_a nothing , mail_a.address <> firstaddress end if end next end sub
p.s.: <> missing on mail_a.address <> firstaddress , mail_b.address <> firstaddress, when posted
Comments
Post a Comment