为循环

时间:2018-02-23 11:25:35

标签: excel vba excel-vba

我需要更正此代码。 表1 = combine, 表2至表10我的数据表。 我希望将所选数据复制到combine表。

我试图做的事情:

使用输入框从sheet2选择数据范围,我想将其他工作表中的相同范围复制到combined

Sub sheetmerge()

Dim combine As Worksheet
Dim arange As Range

    Set combine = Sheets(1)
    Sheets(2).Activate
    Set arange = Application.InputBox(Prompt:="Please select the range", Type:=8)
    arange.Select
    Selection.Copy Destination:=combine.Range("A1000").End(xlUp).Offset(1, 0)

    For x = 3 To Sheets.Count
    Sheets(x).Activate
    Range(arange).Select
    Selection.Copy Destination:=combine.Range("A1000").End(xlUp).Offset(1, 0)
    Next x

End Sub

1 个答案:

答案 0 :(得分:2)

试试这个:

Sub SheetMerge()
    Dim combine As Worksheet, rngToCopy As Range

    Set combine = Worksheets(1)
    Sheets(2).Activate
    Set rngToCopy = Application.InputBox(Prompt:="Please select the range", Type:=8)
    rngToCopy.Copy Destination:=combine.Range("A1000").End(xlUp).Offset(1, 0)

    For x = 3 To Worksheets.Count
        Worksheets(x).Range(rngToCopy.Address).Copy Destination:=combine.Range("A1000").End(xlUp).Offset(1, 0)
    Next x
End Sub