有条件地在一张纸上搜索并将行复制到另一张纸上

时间:2012-03-07 01:58:28

标签: excel vba excel-vba

我有一个名为Open Case Report.xlsm的工作簿,其中包含名为RAW_DataFormatted的工作表。

我想创建一个宏,在{E}栏中搜索一组2个名称,并将整行复制到特定地点RAW_Data的{​​{1}}。Formatted

我在这里看了一下,发现了一些类似的代码,但我似乎无法调整代码来做我想做的事情而不会出现调试错误。

1 个答案:

答案 0 :(得分:1)

您可以使用MATCH方法查找值。之后,您可以使用 Rw 将数据传输到另一张表:

Sub FindRowTransferData()
Dim Rw As Long, myVAL As String

myVAL = Application.InputBox("Enter search value:", "Search", "John Doe", Type:=2)
If myVAL = "False" Then Exit Sub

On Error Resume Next
Rw = Application.WorksheetFunction.Match(myVAL, Sheets("RAW_Data").Range("E:E"), 0)
On Error Goto 0
If Rw = 0 Then
    MsgBox "The search value '" & myVAL & "' was not found"
    Exit Sub
End If

'MsgBox "The search value '" & myVAL & "' was found on row: " & Rw

With Sheets("Formatted")
    .Range("B3").Value = Sheets("RAW_Data").Range("A" & Rw).Value   'name
    .Range("B4").Value = Sheets("RAW_Data").Range("B" & Rw).Value   'address
    .Range("C3").Value = Sheets("RAW_Data").Range("C" & Rw).Value   'phone
    'etc....
End With

End Sub

根据以下评论,这些建议编辑:

Rw = Application.WorksheetFunction.Match(myVAL, Sheets("RAW_Data").Range("F:F"), 0)
On Error Goto 0
If Rw = 0 Then
    MsgBox "The search value '" & myVAL & "' was not found"
    Exit Sub
End If

'MsgBox "The search value '" & myVAL & "' was found on row: " & Rw

Sheets("RAW_Data").Rows(Rw).Copy Sheets("Formatted").Range("A" & Rows.Count).End(xlUp).Offset(1)