Excel VBA – 工作表类的复制方法失败 – 用于正常工作

我有一个时间卡工作簿,在其中,你可以创build一个新的卡从前一周卡。 直到最近才find工作。 我目前有26张表,没有代码已经改变,但它抛出了运行时错误和重点,

ActiveSheet.Copy After:=Sheets.Count 

这是整个模块:

 Sub create_timecard() 'copy sheet afterlast sheet Dim timeCardNo As Integer Dim user As String Dim colCount As Integer Dim topRow As Integer Dim rightCol As Integer Dim cardName As String Dim timeCard As ListObject Dim msgBoxResult As VbMsgBoxResult Dim weekStart As String Dim weekEnd As String Dim curMonth As String Dim endMonth As String msgBoxResult = MsgBox("Would you like to keep the previous week's projects?", vbExclamation + vbYesNoCancel, "New Time Card") If msgBoxResult = vbCancel Then Exit Sub End If 'Get current week curMonth = MonthName(Month(Date), True) weekStart = Day(Date - Weekday(Date, vbMonday) + 1) weekEnd = Day(Date - Weekday(Date, vbMonday) + 7) endMonth = MonthName(Month(Date - Weekday(Date, vbMonday) + 7), True) ActiveSheet.Copy After:=Sheets.Count user = Application.UserName Set timeCard = ActiveSheet.ListObjects(1) timeCardNo = ThisWorkbook.Sheets.Count - 2 'minus one to remove summary sheet from count cardName = user & timeCardNo ActiveSheet.Name = cardName topRow = timeCard.HeaderRowRange.Row - 1 rightCol = timeCard.ListColumns.Count Cells(topRow, rightCol).Value = cardName Cells(topRow, timeCard.ListColumns("Monday").Index).Value = curMonth & " " & weekStart & " - " & endMonth & " " & weekEnd If msgBoxResult = vbNo Then Range(timeCard.DataBodyRange(1, 1), timeCard.DataBodyRange(timeCard.DataBodyRange.Rows.Count, timeCard.DataBodyRange.Columns.Count - 1)).ClearContents ElseIf msgBoxResult = vbYes Then Range(timeCard.DataBodyRange(1, timeCard.ListColumns("Monday").Index), timeCard.DataBodyRange(timeCard.DataBodyRange.Rows.Count, timeCard.DataBodyRange.Columns.Count - 1)).ClearContents End If End Sub 

我在这里search了一个网站,我似乎无法得到任何工作,运行同一工作表的其他员工没有问题。 我正在运行Office 2013.我不知道这是否与此有关,但它是唯一的区别,我有一个Personal.xlsb全局macros。

提前致谢。

该代码应该从来没有工作,争论期待一个表单对象 – 所以它应该是:

 ActiveSheet.Copy After:=Sheets(Sheets.Count) 

试试下面。 它会工作

 ActiveSheet.Copy After:=Sheets(Sheets.Count)