在EXCEL VBA中需要帮助匹配和复制

时间:2014-02-22 20:34:43

标签: excel vba excel-vba

我在Excel工作表中工作,我是VBA的新手。我有一种情况,我有2张不同的纸张(“OriginalData1”)和(“VBAFilter”)。我在(“VBAFilter”)工作表中有一个单元格(F3),它有一个公司名称,多次出现在“OriginalData1”中。我想在(“OriginalData1”)表中找到相同的公司名称。如果它在“OriginalData1”中出现4次,我需要在“VBAFilter”中相同地传输4次,如果是匹配则复制整行并将其粘贴到单元格范围内的表(“VBAFilter”)中(F3:O2000“)

这是我的代码:

Sub datafind()

Dim CompanyName As String
Dim finalrow As Integer
Dim i As Integer

Worksheets("VBAFilter").Range("F9:O1000").ClearContents
CompanyName = Sheets("VBAFilter").Range("F3").Value
finalrow = Sheets("OriginalData1").Range("B" & Rows.Count).End(xlUp).Row


For i = 2 To finalrow

 If Worksheets("OriginalData1").Cells(i, 2) = CompanyName Then
    Worksheets("OriginalData1").Range("B" & Rows.Count).Copy
    Worksheets("VBAFilter").Select
    Range("F1000").End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
  End If
Next i

End Sub

1 个答案:

答案 0 :(得分:1)

Sub datafind()

Dim CompanyName As String
Dim finalrow As Integer
Dim i As Integer
Dim j As Integer

j= 9

Worksheets("VBAFilter").Range("F9:O1000").ClearContents
CompanyName = Sheets("VBAFilter").Range("F3").Value
finalrow = Sheets("OriginalData1").Range("B" & Rows.Count).End(xlUp).Row


For i = 2 To finalrow

    If Worksheets("OriginalData1").Cells(i, 2) = CompanyName Then
        Worksheets("OriginalData1").Cells(i, 2).Resize(1,1000).Copy
        Worksheets("OriginalData1").Cells(j, "F").Resize(1,1000).PasteSpecial xlPasteFormulasAndNumberFormats
        j = j + 1
    End If
Next i

End Sub