Usedrange调整大小VBA

时间:2016-04-05 16:10:11

标签: excel vba excel-vba

我使用以下代码从所选文件中的各种Excel工作表中复制和数据,并粘贴到单独的合并数据工作簿。

它工作正常,但我想复制并粘贴工作表中的文本而不包括第一行(或标题)。

我想我需要调整使用范围选择的大小。

任何想法都会受到赞赏。

Sub Data()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range

Set wb1 = ActiveWorkbook
Set PasteStart = [Sheet1!A2]

Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a Report", _
FileFilter:="(.xls),")

If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub

Else

Set wb2 = Workbooks.Open(Filename:=FileToOpen)

For Each Sheet In wb2.Sheets
    With Sheet.UsedRange
        .Copy PasteStart
        Set PasteStart = PasteStart.Offset(.Rows.Count)
    End With
Next Sheet

End If

wb2.Close

End Sub

1 个答案:

答案 0 :(得分:1)

For Each Sheet In wb2.Sheets
    With Sheet.UsedRange
        .Offset(1, 0).Resize(.Rows.Count-1, .Columns.Count).Copy PasteStart
        Set PasteStart = PasteStart.Offset(.Rows.Count-1)
    End With
Next Sheet