将工作表从一个工作簿复制到另一工作簿

时间:2018-09-18 10:54:46

标签: excel vba

试图从当前工作簿“ Create Report.xlsm”中复制工作表名称“标题说明”,该工作簿已打开至我要求打开的工作簿中 我的下标超出范围错误

Private Sub CommandButton2_Click()

Dim myfile As String

myfile = Application.GetOpenFilename(Title:="Please choose a file to open", FileFilter:="Excel Files *.xls* (*.xls*),")

Workbooks.Open Filename:=myfile

Workbooks("Create Report.xlsm").Sheets("Headings Explanations").Copy After:=Workbooks(myfile).Sheets(Sheet.Count)

End Sub

1 个答案:

答案 0 :(得分:0)

myfile 是完整的驱动器,文件夹路径和文件名。要引用Workbooks集合中打开的工作簿,您只需要文件名。将工作簿var设置为打开的工作簿,然后改用它。

Sheet.count是单数;应该是workSheets.count或sheets.count。

Private Sub CommandButton2_Click()

    Dim myfile As String, wbo as workbook

    myfile = Application.GetOpenFilename(Title:="Please choose a file to open", FileFilter:="Excel Files *.xls* (*.xls*),")

    set wbo = Workbooks.Open(Filename:=myfile)

    Workbooks("Create Report.xlsm").workSheets("Headings Explanations").Copy After:=wbo.workSheets(workSheets.Count)

End Sub