使用VBA将工作表复制到其他工作簿

时间:2018-07-09 13:44:40

标签: excel vba

下面的代码行出现“错误9:下标超出范围”

ActiveWorkbook.Worksheets("Daily Allocation").Copy After:=Workbooks("\\Bmcstr01\grp\SRV\Allsrv\NEW Complaints Logger\GI Complaints\Spreadsheets\Archieve\Access_Log.xlsx").Sheets(Workbooks("\\Bmcstr01\grp\SRV\Allsrv\NEW Complaints Logger\GI Complaints\Spreadsheets\Archieve\Access_Log.xlsx").Sheets.Count)

完整代码:

Sub DASaveAs()
'MsgBox "SaveAs"
Dim namedate As String
namedate = Date
Application.ScreenUpdating = False

ActiveWorkbook.Worksheets("Daily Allocation").Select
ActiveWorkbook.Worksheets("Daily Allocation").Copy After:=Workbooks("\\Bmcstr01\grp\SRV\Allsrv\NEW Complaints Logger\GI Complaints\Spreadsheets\Archieve\Access_Log.xlsx").Sheets(Workbooks("\\Bmcstr01\grp\SRV\Allsrv\NEW Complaints Logger\GI Complaints\Spreadsheets\Archieve\Access_Log.xlsx").Sheets.Count)
ActiveSheet.Name = namedate
'Workbooks.Open Filename:="\\Bmcstr01\grp\SRV\Allsrv\2011 CRU\COMPLAINTS ADMIN FOLDER\Daily Allocation.xlsx"
'ActiveWorkbook.Worksheets("Daily Allocation").SaveAs ("\\Bmcstr01\grp\SRV\Allsrv\NEW Complaints Logger\GI Complaints\Spreadsheets\Archieve\ER Complaints - " & Format(Now(), "DD.MM.YYYY hhmm AMPM") & ".xlsx")
ActiveWorkbook.Close
Application.ScreenUpdating = True
End Sub

有人可以帮忙吗?

谢谢, 弥勒佛

1 个答案:

答案 0 :(得分:0)

除非已打开,否则无法将其复制到工作簿。打开后,其名称将不包含文件的路径。所以

 Workbooks("\\Bmcstr01\grp\SRV\Allsrv\NEW Complaints Logger\GI Complaints\Spreadsheets\Archieve\Access_Log.xlsx")

应该是

Workbooks("Access_Log.xlsx")