Hey all
I have a workbook that has a number of cover sheets and then a bunch of sheets at the back that are contain a few graphs. The graph pages are created by copy-pasting one sheet ("MasterFormat") over and over again, changing a few key values each time.
The macro originally used to conk out fairly rapidly with a Copy Method of Worksheet Class failed
error. I eventually found how to fix it, from http://support.microsoft.com/kb/210684 .
The problem is, I've had endless issues with my updated version; mostly that it continues running happily, but doesn't actually copy anything after a while. Part of why it's happy is that the updated logic includes a few Set x = y, if x is nothing then
s, which (as far as I know) will only work with errors suppressed, so that's what I've done. But on the other hand, it stops copying sheets after there are 50 sheets, and gives no explanation (though this may be the mislocation of the on error goto 0
).
Does anyone know what I should be fixing to make it actually copy all the sheets, not just get bored and stop?
The code is as follows:
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
It's run from a meta workbook, which was the suggestion of the KB article I linked to above. Interestingly, despite the Open workbook
, it doesn't seem to actually work if the main workbook is not open.