0

我没有 10 名声望,所以我无法上传图片,这会使解释变得容易得多……我在此处的 Excel 先生论坛上发布了一个表格示例:http://www.mrexcel。 com/forum/excel-questions/833202-visual-basic-applications-find-non-match-criteria-ws2-copy-criteria-ws1.html

我有两个工作表。我需要在“Sheet2”中找到与“Sheet1”中的员工 ID 匹配的员工 ID。如果“Sheet2”的 ID 不在“Sheet1”中,那么我需要将特定单元格从“Sheet2”的所述行复制到“Sheet1”。

最重要的是,在复制时,我需要确保为复制的单元格插入一整行,以便之前的 $amounts 位于正确的位置(请参阅 Excel 先生中的帖子)。这可能没有任何意义。要是能上传图片就好了……

Option Explicit

Sub CopyNonMatches()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim ws1Range As Range, ws2range As Range
Dim ws1Long As Long, ws2long As Long

Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

With ws1
ws1Long = .Range("C" & .Rows.Count).End(xlUp).Row
End With

Set ws1Range = ws1.Range("C3", "C" & ws1Long)

With ws2
ws2long = .Range("C" & .Rows.Count).End(xlUp).Row
End With

Set ws2range = ws2.Range("C3", "C" & ws2long)

'Now I need to compare the ranges 'ws1Range' with 'ws2Range' and if 'ws2Range' has ID's that...
'...are not included in 'ws1Range', then I need to copy over info from Columns A to C from the row that had no match over to 'Sheet1'.

???????????????????

End Sub
4

1 回答 1

0

子 CopyNonMatches()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim vIDs1 As Variant, vData As Variant
Dim i As Long

Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

vIDs1 = ws1.Range("C2", ws1.Range("C" & Rows.Count).End(xlUp)).Value
vData = ws2.Range("A2", ws2.Range("C" & Rows.Count).End(xlUp)).Value

For i = 1 To UBound(vData, 1)
    If IsError(Application.Match(vData(i, 3), vIDs1, 0)) Then
        ws1.Rows(8).Insert
        ws1.Range("A8:C8").Value = Array(vData(i, 1), vData(i, 2), vData(i, 3))
    End If
Next i

结束子

于 2015-02-03T04:21:13.240 回答