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

时间:2016-07-13 15:10:36

标签: excel vba excel-vba

我有一张时间卡工作簿,你可以在上面的周卡中创建一张新卡。直到最近才开始工作。我目前有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

我在这里搜索了一个网站,我似乎无法正常工作,其他正在运行相同工作表的员工也没有问题。我正在运行Office 2013.而且我不知道这与它有什么关系,但它是唯一的区别,我有一个Personal.xlsb用于全局宏。

提前致谢。

2 个答案:

答案 0 :(得分:4)

该代码应该从未起作用,该参数期望一个工作表对象 - 所以它应该是:

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

答案 1 :(得分:1)

尝试下面的内容。它会起作用

ActiveSheet.Copy After:=Sheets(Sheets.Count)
相关问题