简体   繁体   中英

excel vba use command if

I have two excel files (file 1 with sheet 1 and file 2 with sheet 2). Both have inside, in column A all the names (for example in A1: "a", in A2: "b" etc etc), in column B the values of relative lenghts (for example in B1: "2", in B2: "3" etc etc) but the orders of the things in the two sheets are different; so, I would want to say: "if the name in a cell in column A of sheet 1 is the same of the name in a cell in column A of sheet 2, so compare the relative values in column B and write TRUE in column C of one of the sheet (if the values are identical) or FALSE" for you is possible? could you help me, please? thanks in advance

If Sheet1 has

a    5
b    6
c    7
d    8

and Sheet2 has

c    3
d    8
a    5

then you can type

=IFERROR(IF(B1=INDIRECT("Sheet2!B"&MATCH(A1,Sheet2!A$1:A$4,0)),"Match Found",""),"")

into cell C1 of Sheet1 and drag the fill handle down and you will get

Match Found


Match Found

in column C of Sheet1. No VBA needed. :)

Note: Since your second sheet is actually in a separate file, you'll want to change Sheet2 to [file2.xlsx]Sheet1 in cell C1.

If you want to avoid INDIRECT function then try the following:

Assuming your Sheet1 of file1 is as follows:

在此处输入图片说明

And Sheet2 of another file is like:

在此处输入图片说明

Enter the following formula in Cell C1 of `Sheet2' and drag/copy it down as required.

=IFERROR(IF(B1=INDEX([file1.xlsx]Sheet1!$B$1:$B$5,MATCH(A1,[file1.xlsx]Sheet1!$A$1:$A$5,0)),TRUE),FALSE)

仅当我比较两张纸的同一个单元格并且发现该单元格的名称中只有一部分相同时,我才想复制TRUE(因为实际上在单元格中不仅有a,b, c但一个句子例如为“ Sample 1 SX arrow”,其他句子为“ Sample 1 DX arrow”),所以我只想在C列中输入TRUE字样,前提是A列中的不完整名称相同但仅一部分句子(仅对“ SX箭头”部分起作用!!如何在循环中添加它?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM