2017-08-03 109 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)。 –