【问题标题】:VBA excel, finding corresponding values in columnsVBA excel,在列中查找相应的值
【发布时间】:2017-06-12 13:09:04
【问题描述】:

我已经设法遍历两列以查找一列中的值是否存在于另一列中。

现在下一步是确定两列中存在的值是否在值右侧的单元格中包含相同的值。

期望结果的图片和当前结果应该说明我想要达到的目标。

附:不要被代码混淆,因为我的工作簿中的列位于两个不同的工作表上。

一旦我得到匹配的确认,我已经尝试在列中循环两次,但在这一点上,我只是输了......对不起

Sub loopDb()

    Set dbsheet1 = ThisWorkbook.Sheets("Sheet1")
    Set dbsheet2 = ThisWorkbook.Sheets("Sheet2")

    lr1 = dbsheet1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = dbsheet2.Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To lr1
        act1 = dbsheet1.Cells(x, 1)

        For y = 2 To lr2
            act2 = dbsheet2.Cells(y, 1)

            If Not dbsheet2.Cells(y, 3).Value = "Match" Then
                'Only compare if previoulsy not done or resulted in "No match"
                If act2 = act1 Then
                    dbsheet2.Cells(y, 3).Value = "Match"

                    If dbsheet2.Cells(y, 3).Value = "Match" Then
                        For i = 2 To lr1
                            If dbsheet2.Cells(y, 1).Value = dbsheet2.Cells(i, 1).Value Then
                                dbsheet2.Cells(y, 4).Value = "Match"
                            Else
                                dbsheet2.Cells(y, 4).Value = "No match"
                            End If
                        Next i
                    End If

                 Else
                 dbsheet2.Cells(y, 3).Value = "No match"
                End If
            End If
        Next y

    Next x


End Sub

【问题讨论】:

  • 为什么不在你的逻辑中使用application.worksheetfunction.match(col1value,col2,0)AND,所以if match1 AND match2 then

标签: vba excel


【解决方案1】:

正如 Nathan_Sav 指出的那样,您可以使用匹配公式解决您的问题。

这个MATCH formula 可以让你完全不用vba。

col1 和 col2 的匹配公式:

=IFERROR(IF(MATCH(sheet1!A1;sheet2!$A$1:$A$10;0)>=0;"Match");"No match")

匹配公式返回找到匹配项的索引。否则出错。要获得“匹配”和“不匹配”这两个词,我们需要 IFIFERROR 公式。

col1 & val1 和 col2 & val 2 的匹配公式

{=IFERROR(IF(MATCH(sheet1!A1&sheet1!B1;sheet2!$A$1:$A$10&sheet2!$B$1:$B$10;0)>=0;"Match");"No match")}

组合 sheet2 的两列需要使用数组公式。为了使其工作,请按 Ctrl+Shift+Enter。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2021-10-13
    • 2014-06-08
    • 2015-04-28
    相关资源
    最近更新 更多