Visual Basic to Print Current Page in Excel

marycullen

New Member
Joined
Apr 7, 2006
Messages
10
Excel 2003

Using Visual Basic I am creating a Project Tracking report using SubTotals. I am using the option "Page Break Between Groups" as the user wants be able to print details for a single Project at a time. Excel doesn't have a "Current" option in the print menu like Word so I don't know how to code to print just the page the user is current on.

I would appreciate any help.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I found this
http://www.ozgrid.com/forum/showthread.php?t=28860

Code:
Public Sub Print_Page_of_ActiveCell()
    Dim ActiveRow As Long, ActiveCol As Integer
    Dim iHPBs As Integer, iVPBs As Integer
    Dim iRow As Integer, iCol As Integer, iPage As Integer
     
    ActiveRow = ActiveCell.Row
    ActiveCol = ActiveCell.Column
    ActiveSheet.UsedRange
    If IsEmpty(ActiveCell.SpecialCells(xlCellTypeLastCell)) Then _
    ActiveCell.SpecialCells(xlCellTypeLastCell).FormulaR1C1 = " "
    If ActiveRow > ActiveCell.SpecialCells(xlCellTypeLastCell).Row Or _
    ActiveCol > ActiveCell.SpecialCells(xlCellTypeLastCell).Column Then _
    Exit Sub
     
    With ActiveSheet
        iHPBs = .HPageBreaks.Count
        iVPBs = .VPageBreaks.Count
        If iHPBs = 0 And iVPBs = 0 Then GoTo PrintSheet
Horizontal:
        For iRow = iHPBs To 1 Step -1
            If .HPageBreaks(iRow).Location.Row <= ActiveRow Then GoTo Vertical
        Next iRow
Vertical:
        For iCol = iVPBs To 1 Step -1
            If .VPageBreaks(iCol).Location.Column <= ActiveCol Then GoTo PrintSheet
        Next iCol
PrintSheet:
        iPage = (iRow + 1) + (iCol * (iHPBs + 1))
        .PrintOut From:=iPage, To:=iPage
        MsgBox "Printing page " & iPage
    End With
    If ActiveCell.SpecialCells(xlCellTypeLastCell).FormulaR1C1 = " " Then _
    Selection.SpecialCells(xlCellTypeLastCell).ClearContents
End Sub

HTH
Cal
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

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