Excel VBA不等于不工作

您好在另一个论坛上发现了一个macros,解决了我的问题:它复制了Sheet 2中匹配Sheet 1的单元格。

Sub Compare()

Dim WS As Worksheet Set WS = Sheets("Master") Dim RowsMaster As Integer, Rows2 As Integer Dim Rows3 As Integer RowsMaster = WS.Cells(1048576, 1).End(xlUp).Row Rows2 = Worksheets(2).Cells(1048576, 1).End(xlUp).Row Rows3 = WS.Cells(WS.Rows.Count, 1).End(xlUp).Row ' Get the number of used rows for each sheet ScreenUpdating = False With Worksheets(2) For i = 2 To Rows2 ' Loop through Sheet 2 For j = 2 To RowsMaster ' Loop through the Master sheet 'below line checks for matches to variable number, file and field If .Cells(i, 1) = WS.Cells(j, 1) And .Cells(i, 3) = WS.Cells(j, 3) And .Cells(i, 4) = WS.Cells(j, 4) Then ' If a match is found: WS.Cells(j, 6) = .Cells(i, 1) WS.Cells(j, 7) = .Cells(i, 2) WS.Cells(j, 8) = .Cells(i, 3) WS.Cells(j, 9) = .Cells(i, 4) ' above copies in the data Exit For ' No point in continuing the search for that company ElseIf j = RowsMaster Then ' If we got to the end of the Master sheet ' and haven't found a company match Rows3 = Rows3 + 1 ' Increment the number of rows For k = 1 To 4 'For l = 2 To 4 ' Change 3 to however many fields Sheet2 has WS.Cells(Rows3, 11) = .Cells(i, k) ' Copy the data from Sheet2 in on the bottom row of Master Next End If Next j Next i End With ScreenUpdating = True End Sub 

此代码将从sheet2到master的值进行匹配。 不匹配的结果只在K6单元格中 – 我需要的是在K列(K列的4列)中进行匹配的列表,然后每行列出一列。

任何人都可以帮我这个 – 一直工作了2天,不能得到它的工作!

谢谢。