将工作表2的整个行(其中包含工作表1的A列的任何值)复制到工作表3中

时间:2018-08-07 08:04:12

标签: excel excel-vba excel-formula

我希望能够复制工作表2中的任何行,其中包含工作表1中列a的任何值。复制并粘贴到工作表3中。

我在网上找到了此代码,但是单元格的值是特定的。我大约有80个值,因此单独列出它们将花费很长时间。

Sub Test()
For Each Cell In Sheets(1).Range("J:J")
    If **Cell.Value = "131125"** Then
        matchRow = Cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy

        Sheets("Sheet2").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
    End If
Next
End Sub

1 个答案:

答案 0 :(得分:0)

如何?

Option Explicit
Sub CopyThings()
    Dim rng As Range
    Dim rng1 As Range
    Dim ans As Integer
    On Error GoTo ISAIDRANGE
    Set rng = Application.InputBox("what do you want to copy?", "Select Range", Type:=8)
    ans = MsgBox("the whole row?", vbYesNo)
    Set rng1 = Application.InputBox("where do you want to paste", "Select Range", Type:=8)
    Application.ScreenUpdating = False
    rng1.Parent.Activate
    Select Case ans
        Case Is = vbYes
            rng.Rows.EntireRow.Copy rng1.Rows.EntireRow
        Case Is = vbNo
            rng.Copy rng1
    End Select
ISAIDRANGE:
    Application.ScreenUpdating = True
    If Err.Number = 424 Then ans = MsgBox("that's not a valid range", vbExclamation, "I meant a VALID range")
End Sub