VBA将第一个工作表从已关闭的工作簿导入到活动工作簿

时间:2017-06-28 16:44:02

标签: vba

我无法修改此代码以复制已关闭工作簿的第一个工作表并将其导入活动工作簿。它希望复制所有工作表,然后添加"之后:= WB.Sheets(WB.Sheets.Count)"到表格中的随机单元格。 非常感谢任何帮助。

Dim WB As Workbook
Dim SourceWB As Workbook
Dim WS As Worksheet
Dim ASheet As Worksheet

Application.ScreenUpdating = False
Application.EnableEvents = False

Set WB = ActiveWorkbook
Set ASheet = ActiveSheet
Set SourceWB = Workbooks.Open("C:\Users\ME\Desktop\Book1.xlsx") 

For Each WS In SourceWB.Worksheets
    WS.Copy after:=WB.Sheets(WB.Sheets.Count)
Next WS

    SourceWB.Close savechanges:=False
    Set WS = Nothing
    Set SourceWB = Nothing

WB.Activate
ASheet.Select
    Set ASheet = Nothing
    Set WB = Nothing

Application.EnableEvents = True

1 个答案:

答案 0 :(得分:0)

试试这个:

Option Explicit

Public Sub copyFirstWS()
    Dim wb As Workbook

    Set wb = Workbooks.Open("C:\Users\ME\Desktop\Book1.xlsx")

    With ThisWorkbook
        wb.Worksheets(1).Copy After:=.Worksheets(.Worksheets.Count)
    End With

    wb.Close savechanges:=False
End Sub