menschmaschine
New Member
- Joined
- Dec 21, 2022
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
The code below is meant to insert pagebreaks at certain rows based on the cell value in column A = "^". On Print Preview, it has the right number of pages, but just repeats the first page on all pages, instead of printing the area for each pagebreak. When I do this manually, it works fine, but I don't understand why the code makes it repeat the first page for all pages. Anybody have an idea what's wrong here?
VBA Code:
Sub Print_UnitSchedule()
Dim USch As Worksheet
Dim ShRowLast As Long, i As Long
Dim ShCol As Range, PRngStrt As Range, PRngEnd As Range, UPrtRng As Range
Application.ScreenUpdating = False
Set USch = Sheets("Unit Schedule")
USch.ResetAllPageBreaks
Set PRngStrt = USch.Range("A7")
ShRowLast = USch.Range("A" & Rows.Count).End(xlUp).Row
Set PRngEnd = USch.Range("AE" & ShRowLast)
Set UPrtRng = Range(PRngStrt, PRngEnd)
Set ShCol = USch.Range("A7:A" & ShRowLast)
For i = 7 To ShRowLast
If ShCol.Cells(i, 1).value = "^" Then
USch.HPageBreaks.Add Before:=ShCol.Cells(i, 1)
End If
Next i
With USch.PageSetup
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.Orientation = xlLandscape
.CenterHorizontally = True
.Zoom = False
.FitToPagesWide = 1
End With
UPrtRng.PrintPreview
USch.ResetAllPageBreaks
Application.ScreenUpdating = True
End Sub