我有一个工作簿,其中有许多封面,然后是一堆包含一些图表的背面的工作表。图形页面是通过一次又一次地复制粘贴一个工作表(“MasterFormat”)创建的,每次都会更改几个键值。
最初用于快速调出Copy Method of Worksheet Class failed
错误的宏。我最终从http://support.microsoft.com/kb/210684找到了解决方法。
问题是,我的更新版本存在无穷无尽的问题;主要是它继续快乐地运行,但一段时间后实际上并没有复制任何东西。令人高兴的部分原因是更新的逻辑包含一些Set x = y, if x is nothing then
s,(据我所知)只会在抑制错误的情况下工作,这就是我所做的。但另一方面,它会在有50张纸之后停止复印纸张,并且没有给出任何解释(尽管这可能是on error goto 0
的错位)。
有没有人知道我应该修理什么才能让它真正复制所有表格,而不仅仅是感到无聊并停止?
代码如下:
Sub GenerateSheets()
Application.ScreenUpdating = False
Dim oBook As Workbook
On Error Resume Next
Set oBook = Workbooks("SSReport.xls")
If oBook Is Nothing Then
Set oBook = Application.Workbooks.Open("SSReport.xls")
End If
On Error GoTo 0
Dim i, j As Integer
Dim SheetName As String
Dim ws As Worksheet
Const PairingCount = 63
Dim Pairings(1 To PairingCount, 1 To 2) As String
For i = 1 To PairingCount
Pairings(i, 1) = oBook.Sheets("SSPairings").Rows(i + 1).Cells(1)
Pairings(i, 2) = oBook.Sheets("SSPairings").Rows(i + 1).Cells(2)
Next i
For i = 1 To PairingCount
If i Mod 5 = 0 Then
oBook.Close SaveChanges:=True
Set oBook = Nothing
Set oBook = Application.Workbooks.Open("SSReport.xls")
End If
Application.ScreenUpdating = False
j = oBook.Worksheets.Count
SheetName = "P" & Pairings(i, 1) & Pairings(i, 2)
On Error Resume Next
Set ws = oBook.Sheets(SheetName)
If ws Is Nothing Then
On Error GoTo 0
oBook.Sheets("MasterFormat").Copy After:=Sheets(j)
oBook.Sheets("MasterFormat (2)").Name = SheetName
End If
oBook.Sheets(SheetName).Cells(1, 2) = Pairings(i, 1)
oBook.Sheets(SheetName).Cells(1, 5) = Pairings(i, 2)
oBook.Sheets(SheetName).Cells(1, 8) = "P"
Next i
Application.ScreenUpdating = True
End Sub
它是从元工作簿运行的,这是我上面链接的知识库文章的建议。有趣的是,尽管有Open workbook
,但如果主工作簿没有打开,它似乎并没有真正起作用。
答案 0 :(得分:1)
错误可能是由以下行引起的:
oBook.Sheets("MasterFormat").Copy After:=Sheets(j)
Sheets(j)
将引用代码模块所在的任何工作簿,这可能不是预期的工作簿。
以下适用于我:
Sub GenerateSheets()
Dim oBook As Workbook
Dim i As Long
Dim j As Long
Dim SheetName As String
Dim ws As Worksheet
Const PairingCount = 63
Dim Pairings(1 To PairingCount, 1 To 2) As String
On Error Resume Next
Set oBook = Workbooks("SSReport.xls")
On Error GoTo 0
If oBook Is Nothing Then
Set oBook = Application.Workbooks.Open("SSReport.xls")
End If
With oBook
For i = 1 To PairingCount
Pairings(i, 1) = .Sheets("SSPairings").Rows(i + 1).Cells(1)
Pairings(i, 2) = .Sheets("SSPairings").Rows(i + 1).Cells(2)
Next i
For i = 1 To PairingCount
If i Mod 5 = 0 Then
'//Save in case of corruption/error?'
.Save
End If
j = .Worksheets.Count
SheetName = "P" & Pairings(i, 1) & Pairings(i, 2)
On Error Resume Next
Set ws = .Sheets(SheetName)
On Error GoTo 0
If ws Is Nothing Then
.Sheets("MasterFormat").Copy After:=.Sheets(j)
.Sheets("MasterFormat (2)").Name = SheetName
End If
.Sheets(SheetName).Cells(1, 2) = Pairings(i, 1)
.Sheets(SheetName).Cells(1, 5) = Pairings(i, 2)
.Sheets(SheetName).Cells(1, 8) = "P"
Next i
End With
End Sub
我冒昧用简单的Save
取代关闭/重新打开,因为这应该会得到相同的结果?
答案 1 :(得分:0)
尝试更改
If ws Is Nothing Then
On Error GoTo 0
oBook.Sheets("MasterFormat").Copy After:=Sheets(j)
oBook.Sheets("MasterFormat (2)").Name = SheetName
End If
oBook.Sheets(SheetName).Cells(1, 2) = Pairings(i, 1)
oBook.Sheets(SheetName).Cells(1, 5) = Pairings(i, 2)
oBook.Sheets(SheetName).Cells(1, 8) = "P"
进入
If ws Is Nothing Then
On Error GoTo 0
oBook.Sheets("MasterFormat").Copy After:=Sheets(j)
oBook.Sheets("MasterFormat (2)").Name = SheetName
else
oBook.Sheets(SheetName).Cells(1, 2) = Pairings(i, 1)
oBook.Sheets(SheetName).Cells(1, 5) = Pairings(i, 2)
oBook.Sheets(SheetName).Cells(1, 8) = "P"
End If
我猜如果ws什么都没有,那么就会陷入接下来的3行。
答案 2 :(得分:0)
根据Lunatik的回答,我将oBook.Sheets("MasterFormat").Copy After:=Sheets(j)
更改为oBook.Sheets("MasterFormat").Copy After:=oBook.Sheets(j)
,这似乎可以解决问题。