Modification of Code

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
Allen Wyatt (on his site) has created the below Code and it works GREAT.

Code:
Sub PageBreak()
    Dim CellRange As Range
    Dim TestCell As Range

    Set CellRange = Selection
    For Each TestCell In CellRange
        ActiveSheet.Rows(TestCell.Row).PageBreak = xlPageBreakNone
        If TestCell.Value <> TestCell.Offset(-1, 0).Value Then
            ActiveSheet.Rows(TestCell.Row).PageBreak = xlPageBreakManual
        End If
    Next TestCell
    With ActiveSheet
        .PrintPreview
    End With
End Sub

Is there anyway it can be MODIFIED (the above) to On Each Previewd Page (Grouped BY Dept) that I can Create a Page Header and in it
Reference the "Department Field Name" being Currently Printed (below on that page)?

Thanks in Advance...

Jim
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How about ...

Code:
Sub PrintByDept()
  Dim wks           As Worksheet
  Dim rBeg          As Range
  Dim cell          As Range
  Dim sDept         As String

  Set wks = ActiveSheet
  wks.Cells.PageBreak = xlPageBreakNone

  With Intersect(ActiveWindow.RangeSelection, wks.UsedRange)

    Set rBeg = .Cells(1)
    sDept = rBeg.Value

    For Each cell In .Cells
      If cell.Value <> sDept Then
        With wks.PageSetup
          .PrintArea = Intersect(Range(rBeg, cell.Offset(-1)).EntireRow, wks.UsedRange).Address
          .LeftHeader = sDept
        End With
        wks.PrintPreview
        Set rBeg = cell
        sDept = rBeg.Value
      End If
    Next cell
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top