jimrward
Well-known Member
- Joined
- Feb 24, 2003
- Messages
- 1,897
- Office Version
- 2021
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
- 2003 or older
- Platform
- Windows
since excel is awkward at printing and insists on a separate sheet for each print
does anyone have some ready made code to loop thru all worksheets in a workbook and copy the print area from each, with the following rules
will not always be the same size area
may or may not exist
if it exists then paste the area to a single new worksheet so that it can be printed, with 2 lines between each area for clarity, I can add pagebreaks manually
I found the following and tried to modify for print areas but it keeps blowing up excel with out of memory and trying to report back to the microsoft mothership, and on the extra sheet it seems to work find for the first area and then for the second it gets it wrong and pastes the wrong bit
does anyone have some ready made code to loop thru all worksheets in a workbook and copy the print area from each, with the following rules
will not always be the same size area
may or may not exist
if it exists then paste the area to a single new worksheet so that it can be printed, with 2 lines between each area for clarity, I can add pagebreaks manually
I found the following and tried to modify for print areas but it keeps blowing up excel with out of memory and trying to report back to the microsoft mothership, and on the extra sheet it seems to work find for the first area and then for the second it gets it wrong and pastes the wrong bit
Code:
Sub PrintOnePage()
Dim wshTemp As Worksheet, wsh As Worksheet
Dim rngArr() As Range, c As Range
Dim i As Integer
Dim j As Integer
ReDim rngArr(1 To 1)
For Each wsh In ActiveWorkbook.Worksheets
p = wsh.PageSetup.PrintArea
If p <> "" Then
i = i + 1
If i > 1 Then ' resize array
ReDim Preserve rngArr(1 To i)
End If
Set rngArr(i) = wsh.Range(p)
End If
Next wsh
'Add temp.Worksheet
Set wshTemp = Sheets.Add(after:=Worksheets(Worksheets.Count))
On Error Resume Next
With wshTemp
For i = 1 To UBound(rngArr)
If i = 1 Then
Set c = .Range("A1")
Else
Set c = _
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell)
Set c = c.Offset(2, 0).End(xlToLeft) ' skip one row
End If
'Copy-paste range (prevent empty range)
If Application.CountA(rngArr(i)) > 0 Then
rngArr(i).Copy c
End If
Next i
End With
On Error GoTo 0
Application.CutCopyMode = False ' prevent marquies
With ActiveSheet.PageSetup ' Fit to 1 page
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
'Preview New Sheet
ActiveWindow.SelectedSheets.PrintPreview
'Print Desired Number of Copies
i = InputBox("Print how many copies?", "ExcelTips", 1)
If IsNumeric(i) Then
If i > 0 Then
ActiveSheet.PrintOut Copies:=i
End If
End If
'Delete temp.Worksheet?
If MsgBox("Delete the temporary worksheet?", _
vbYesNo, "ExcelTips") = vbYes Then
Application.DisplayAlerts = False
wshTemp.Delete
Application.DisplayAlerts = True
End If
End Sub