VBA将特定范围的值复制到另一个新工作簿

时间:2017-09-25 03:13:03

标签: excel vba excel-vba

https://www.imageupload.co.uk/images/2017/09/25/question.png

请问如何通过单击“CommandButton”让代码自动将A3-I19中的数据范围复制到另一个新工作簿。

Sub CommandButton()
Range("A3").Copy Range("I19")
End Sub

1 个答案:

答案 0 :(得分:0)

将波纹管指定给命令按钮。

Sub CommandButton()
    Dim wb As Workbook
    Set wb = Workbooks.Add(xlWBATWorksheet)

    Dim sh As Long
    Dim rg As Range
    Dim Rng As Range
    Dim rw As Range

    Application.ScreenUpdating = False

    For Each rw In Range("A3:I19").Cells
    If rw.EntireRow.Hidden = False Then
        If Rng Is Nothing Then Set Rng = rw
        Set Rng = Union(rw, Rng)
    End If

    Next

    Rng.Copy

    With wb.Sheets("Sheet1")

     sh = .Cells(Rows.Count, "A").End(xlUp).row + 2

           .Activate

       .Cells(sh, "A").Select
    End with

       ActiveSheet.Paste

End Sub