使用VBA或宏将Sheet1数据从多个工作簿从特定文件夹导入单个工作簿

时间:2019-06-24 08:57:01

标签: excel vba

使用VBA或宏将Sheet1的数据从多个工作簿中提取到单个工作簿中

Option Explicit
Sub MergeExcels()

    Dim Path As String, FName As String
    Dim wb As Workbook
    Dim ws As Worksheet

    Path = ""
    FName = Dir(Path & "*.xlsx")
    With ThisWorkbook
        Do While FName <> ""
            Set wb = Workbooks.Open(Path & FName, ReadOnly:=True)
            For Each ws In wb.Worksheets
                ws.Copy After:=.Sheets(.Sheets.Count)
            Next ws
            wb.Close SaveChanges:=False
            FName = Dir()
        Loop
    End With

End Sub

上面的代码获取工作簿中的所有工作表,但我只需要Sheet1数据

3 个答案:

答案 0 :(得分:0)

更改:

For Each ws In wb.Worksheets
    ws.Copy After:=.Sheets(.Sheets.Count)
Next ws

收件人:

wb.Worksheets("Sheet1").Copy After:=.Sheets(.Sheets.Count)

或者,如果您的意思是第一个工作表而不是一个名为Sheet1的工作表:

wb.Worksheets(1).Copy After:=.Sheets(.Sheets.Count)

答案 1 :(得分:0)

Option Explicit
Sub MergeExcels()
    Dim Path As String, FName As String
    Dim wb As Workbook
    Dim ws As Worksheet

    Path = "D:\BILL'S\Thankam\2019\June\Bills"
    FName = Dir(Path & "*.xlsx")
    With ThisWorkbook
        Do While FName <> ""
            Set wb = Workbooks.Open(Path & FName, ReadOnly:=True)
            wb.Worksheets("Sheet1").Copy After:=.Sheets(.Sheets.Count)
            wb.Close SaveChanges:=False
            FName = Dir()
        Loop
    End With
End Sub

答案 2 :(得分:0)

显式选项 Sub MergeExcels()

Dim Path As String, FName As String
Dim wb As Workbook
Dim ws As Worksheet

Path = "D:\BILL'S\Thankam\2019\June\Bills"
FName = Dir(Path & "*.xlsx")
With ThisWorkbook
    Do While FName <> ""
        Set wb = Workbooks.Open(Path & FName, ReadOnly:=True)
        wb.Worksheets(1).Copy After:=.Sheets(.Sheets.Count)
        wb.Close SaveChanges:=False
        FName = Dir()
    Loop
    End With

结束子