使用VBA宏比较列

时间:2017-08-03 11:20:14

标签: excel vba excel-vba

当列A和列B值匹配时,我想在Column C中插入相应的Column D值。

例如:

列A2等于列B2,现在列C2值在列D2上发布

OR

列A7等于列B3,然后列D3值在列D3上发布

有关详细信息,请参阅屏幕截图,以便了解我要做的事情。

[请点击查看屏幕截图] [1]

我正在尝试的代码如下,但它无法正常工作,它只提供一个单元格值:

Private Sub ForComparing_Click()

Dim ws As Worksheet
Dim cel As Range
Dim lastRowA As Long, lastRowB As Long, lastRowC As Long

Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
    lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row of column A
    lastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row of column B
    lastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row 'last row of column C
    For Each cel In .Range("A2:A" & lastRowA)   'loop through column A
        'check if cell in column A exists in column B
        If WorksheetFunction.CountIf(.Range("B2:B" & lastRowB), cel) = 0 Then
            .Range("D" & cel.Row) = "No Match"
        Else
            .Range("D" & cel.Row) = .Range("C" & cel.Row)
        End If
    Next
End With

End Sub

已编辑1:

请参阅以下代码的输出: Click here to see screen shot

Column A3应该与Column B5进行比较,因为在这种情况下值D是相等的,然后它应该将Column C5值打印到Column D3

此外,它应该Column DColumn A的每个值赋予值,但它会在前4个值之后停止。

感谢您的时间。

已编辑2:

Please see the screen shot

您刚刚编辑的内容完全正确,但我希望为每个Column A值执行此操作。

我想将每个Column A值与Column B进行比较,然后在Column C上复制相应的Column D值。

2 个答案:

答案 0 :(得分:3)

试试这个

Option Explicit

Sub Demo()
    Dim ws As Worksheet
    Dim cel As Range
    Dim lastRowA As Long, lastRowB As Long

    Set ws = ThisWorkbook.Sheets("Sheet2")

    With ws
        lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row of column A
        lastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row of column B
        For Each cel In .Range("A1:A" & lastRowA)   'loop through column A
            'check if cell in column A exists in column B
            If WorksheetFunction.CountIf(.Range("B1:B" & lastRowB), cel) = 0 Then
                .Range("C" & cel.Row) = "No Match"
            Else
                .Range("C" & cel.Row) = cel & " has match in column B"
            End If
        Next
    End With
End Sub

编辑:

Option Explicit

Sub Demo()
    Dim ws As Worksheet
    Dim cel As Range, rngC As Range, rngB As Range
    Dim lastRowA As Long, lastRowB As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row of column A
        lastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row of column B
        For Each cel In .Range("A2:A" & lastRowB)   'loop through column B
            'check if cell in column A exists in column B
            If WorksheetFunction.CountIf(.Range("A2:A" & lastRowB), cel) = 0 Then
                .Range("D" & cel.Row) = "No Match"
            Else
                .Range("D" & cel.Row) = Application.WorksheetFunction.Index(.Range("C2:C" & lastRowB), Application.WorksheetFunction.Match(cel, .Range("B2:B" & lastRowB), 0), 1)
            End If
        Next
    End With
End Sub

参见图片以供参考。

enter image description here

然而,我仍然非常怀疑你想要实现的目标。您只匹配A列中的前4个值,如您所提到的“但它在前4个值后停止”。不过,根据我的解决方案,它会匹配Column AColumn B的4行,如果匹配,则相应的Column C值将显示在Column D中。如果没有匹配,则Column D将显示No Match

答案 1 :(得分:0)

Sub compare()
Dim i As Integer
i = 1

Do While Cells(i, "A").Value <> ""
    If Cells(i, "A").Value <> Cells(i, "B").Value Then
        Cells(i, "C").Value = "No Match"
    Else
        Cells(i, "C").Value = "Match"
    End If


    i = i + 1
Loop

End Sub

声明一个计数器(i),然后设置一个循环来迭代A列。循环将继续进行,直到在A列中找到一个空白的单元格。

在循环内,对于每一行,您比较2个相应的单元格并更改C列中单元格的值。最后,在循环的每次迭代中向i添加1,以便遍历每一行。

使用以下公式时,更简单的方法是没有VBA:

=IF(A1=B1,"Match", "No Match")