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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:43:36  来源:igfitidea点击:

Compare two columns if another two columns are matching in excel macro

excelexcel-vbavba

提问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 (Trueor Falsein column E).

它写结果(TrueFalseE)。

Explanation:

解释:

  1. The main idea is to determine result with formula and then rewrite formula with its result value.
  2. What we do is writing in E2:E6array formula
    =ISNUMBER(MATCH($A$2:$A$6 & "|" & $B$2:$B$6,$C$2:$C$6 & "|" & $D$2:$D$6,0))- it returns Trueif we found, say A2and B2in any row of range C2:D6, e.g. C3and D3.
  3. .Value = .Valuepart rewrites formulas with theirs results
  4. How it works? Formula concatenates columns Aand Band searches result in concatenation of columns Cand D.
  5. Why we're using & "|" &in formula? Imagine following situaltion:
  1. 主要思想是用公式确定结果,然后用其结果值重写公式。
  2. 我们做的是书面E2:E6数组公式
    =ISNUMBER(MATCH($A$2:$A$6 & "|" & $B$2:$B$6,$C$2:$C$6 & "|" & $D$2:$D$6,0))-它返回True,如果我们发现了,说A2B2在范围内的任何一行C2:D6,例如C3D3
  3. .Value = .Value部分用他们的结果重写公式
  4. 这个怎么运作?公式连接列AB,搜索结果连接列CD
  5. 为什么我们& "|" &在公式中使用?想象以下情况:


A          B            C            D
1      234D                    1234D

technically, concatenation of A1 & B1and C1 & D1gives 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 & "|" & B1returns $101|234Dand C1 & "|" & D1returns $10|1234Dand they are not the same, as we need it.

从技术上讲,连接A1 & B1C1 & D1会给你相同的结果:$101234D,但我们清楚地看到没有匹配。这就是为什么我|在连接值时使用分隔符:A1 & "|" & B1返回$101|234DC1 & "|" & D1返回$10|1234D,它们不一样,因为我们需要它。

回答by mcalex

  1. Does it needto be in vba? 2. What do you do to show there is a match or half-match?
  1. 需要在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 中,您可以设置一个宏来执行这些步骤。