2017-08-03 75 views
-5

当列A和列B值匹配时,我想在Column D中插入对应的Column C值。使用VBA宏比较列

例如:

列A2等于列B2,现在列C2值是贴在柱D2

OR

柱A7等于列B3然后列C3的值过帐在列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 A3Column B5比较,因为价值D在这种情况下相等,那么它应该打印Column C5Column D3

此外,它应该给价值Column DColumn A每个值,但后前4个值停止。

谢谢你的时间。

编辑2:

Please see the screen shot

你刚才编辑什么是完全正确的,但我想每个Column A值做到这一点。

我想比较每个Column A的值与Column B,然后相应的Column C值被复制到Column D

+2

向我们展示你已经尝试过的代码,并解释了问题的所在。请记住,您可以录制一个宏让您开始。 – braX

+0

阅读关于'Match'功能 –

回答

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 A 4行至Column B并且如果它匹配,则对应的Column C值将在Column D被显示。如果没有匹配,则Column D将显示No Match

+0

你好Mrig, 我已经编辑了一个问题,你建议,你可以请指导我什么错误我在上面的代码我犯。 非常感谢您的时间。 –

+0

@AmmadAhmed - 代替'.Range( “C” &cel.Row)= CEL& “在B列匹配”''尝试.Range( “d” &cel.Row)= .Range( “d” & cel.Row)' – Mrig

+0

@AmmadAhmed - 我不在我的系统前面,所以没有经过测试。 – Mrig

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中单元格的值。最后,在循环的每次迭代中,将1添加到i,以便遍历每一行。

一种更简单的方法是不使用VBA下面的公式:

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

+0

'i'必须是'长'而不是'整数'。 Excel有更多的行比整数可以处理。无论如何,使用'Integer'也没有什么优势,所以除非需要与旧的API进行通信,否则您应该[**总是**使用'Long'](https://stackoverflow.com/a/26409520/3219613)。 –