vba 如果另外两列在 excel 宏中匹配,则比较两列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22978075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two columns if another two columns are matching in excel macro
提问by devuser
I want to compare two columns in excel if another two columns are matching.
如果另外两列匹配,我想比较 excel 中的两列。
A B C D
1234D 100D
1235D 1234D
122D 1235D
1222D 1222D
First I need to compare col A and C If any matches find in col C then I need to compare B and D are matching. Example I have $10 in A and its ID is 1234D.i need to compare the same value in C. If I found $10 in C but its id is not 1234D I need to show that un matching one in another column.
首先,我需要比较 col A 和 C 如果在 col C 中找到任何匹配项,那么我需要比较 B 和 D 是否匹配。例如,我在 A 中有 10 美元,它的 ID 是 1234D。我需要比较 C 中的相同值。如果我在 C 中找到了 10 美元,但它的 ID 不是 1234D,我需要在另一列中显示不匹配的值。
I can match A and C as below .but I'm confusing on how to compare B and D after that?i'm new to excel vba and appreciate if any one help me to do this.
我可以按如下方式匹配 A 和 C。但是在那之后我对如何比较 B 和 D 感到困惑?我是 excel vba 的新手,如果有人帮助我做到这一点,我将不胜感激。
Function Find_Matches()
Dim CompareRange As Variant, SelectionRange As Variant, x As Variant, y As Variant
' compare the selection.
Sheets("Menu").Activate
Set SelectionRange = Range("A2:A6")
Set CompareRange = Range("C2:C6")
' Loop through each cell in the selection and compare it to
' each cell in CompareRange.
For Each x In SelectionRange
For Each y In CompareRange
If x = y Then x.Offset(0, 4) = True
Next y
Next x
End Function
回答by Dmitry Pavliv
I need to use vba any way.
我需要以任何方式使用 vba。
Use this macro:
使用这个宏:
Sub Find_Matches()
Dim rng As Range
Set rng = Sheets("Menu").Range("A2:A6")
With rng.Offset(, 4) ' write result in column E
.FormulaArray = "=ISNUMBER(MATCH(" & rng.Address & "&""|""&" & _
rng.Offset(, 1).Address & "," & rng.Offset(, 2).Address & _
"&""|""&" & rng.Offset(, 3).Address & ",0))"
.Calculate
.Value = .Value
End With
End Sub
it writes result (True
or False
in column E
).
它写结果(True
或False
列E
)。
Explanation:
解释:
- The main idea is to determine result with formula and then rewrite formula with its result value.
- What we do is writing in
E2:E6
array formula=ISNUMBER(MATCH($A$2:$A$6 & "|" & $B$2:$B$6,$C$2:$C$6 & "|" & $D$2:$D$6,0))
- it returnsTrue
if we found, sayA2
andB2
in any row of rangeC2:D6
, e.g.C3
andD3
. .Value = .Value
part rewrites formulas with theirs results- How it works? Formula concatenates columns
A
andB
and searches result in concatenation of columnsC
andD
. - Why we're using
& "|" &
in formula? Imagine following situaltion:
- 主要思想是用公式确定结果,然后用其结果值重写公式。
- 我们做的是书面
E2:E6
数组公式=ISNUMBER(MATCH($A$2:$A$6 & "|" & $B$2:$B$6,$C$2:$C$6 & "|" & $D$2:$D$6,0))
-它返回True
,如果我们发现了,说A2
和B2
在范围内的任何一行C2:D6
,例如C3
和D3
。 .Value = .Value
部分用他们的结果重写公式- 这个怎么运作?公式连接列
A
和B
,搜索结果连接列C
和D
。 - 为什么我们
& "|" &
在公式中使用?想象以下情况:
A B C D
1 234D 1234D
technically, concatenation of A1 & B1
and C1 & D1
gives you the same result: $101234D
, but we clearly see that there is no match. That's why I'm using |
as delimeter when concatenating values: A1 & "|" & B1
returns $101|234D
and C1 & "|" & D1
returns $10|1234D
and they are not the same, as we need it.
从技术上讲,连接A1 & B1
和C1 & D1
会给你相同的结果:$101234D
,但我们清楚地看到没有匹配。这就是为什么我|
在连接值时使用分隔符:A1 & "|" & B1
返回$101|234D
和C1 & "|" & D1
返回$10|1234D
,它们不一样,因为我们需要它。
回答by mcalex
- Does it needto be in vba? 2. What do you do to show there is a match or half-match?
- 它需要在vba中吗?2. 你怎么做来表明有一场比赛或半场比赛?
Create a new column in F that is a concatenation of A & B. Do the same in column G to concatenate the values in C & D.
在 F 中创建一个新列,它是 A 和 B 的串联。在 G 列中执行相同的操作以串联 C 和 D 中的值。
Then use a formula that uses VLookup to see if looking up the concatenation of A & B returns an 'N/A' error. If this is false, you have a matching column.
然后使用一个使用 VLookup 的公式来查看查找 A 和 B 的连接是否会返回“N/A”错误。如果这是错误的,则您有一个匹配的列。
So, the formula for F2 is
因此,F2 的公式为
'=concatenate(A2, B2)'
the formula for G2 is
G2的公式是
'=concatenate(C2, D2)'
and the formula to do your vlookup is
做你的 vlookup 的公式是
'=IF(ISNA(Vlookup(concatenate(a2, b2), $F:$G$<LastRowOfData>, 2, FALSE)), "", "Matches")
If it needs to be in vba, you could set up a macro to perform these steps.
如果需要在 vba 中,您可以设置一个宏来执行这些步骤。