如何让我的VBA按钮仅在一个选项卡上运行?

时间:2018-06-12 15:41:53

标签: vba excel-vba excel

我写了一个按钮来复制一个范围内的输入,然后逐行粘贴到其他地方的表格中(即在一个名为' end'的单元格上方有一条线的表格中)。但是当它运行时,它也会将输入粘贴在不同标签上的相同单元格中!知道我做错了吗?

    Private Sub CommandButton2_Click()
        Application.ScreenUpdating = False
        Dim Sheet As Worksheet
        For Each Sheet In ThisWorkbook.Worksheets
            If Sheet.Name <> "Definitions" And Sheet.Name <> "fx" And Sheet.Name <> "Needs" Then
                Sheets("sheet1").Range("A9:G9 ").Copy
                Sheet.Cells(Range("end").End(xlUp).Row + 1, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If
        Next
        Application.ScreenUpdating = True
    End Sub

1 个答案:

答案 0 :(得分:2)

如果你想将它从sheet1复制到sheet1上的另一个地方,这将更简单:

Private Sub CommandButton2_Click()

    Application.ScreenUpdating = False

    With Worksheets("sheet1")

        'this will be even faster than the copy method below
        .Cells(.Range("end").End(xlUp).Row + 1,1).Resize(1,7).Value = _
            .Range("A9:G9").Value

        'this is the copy method that is slower
        '.Range("A9:G9").Copy
        '.Cells(.Range("end").End(xlUp).Row + 1,1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    End With


End Sub