[Solved] Define print area with MsgBox and print selected area

bobpru

New member
I have a excel log that I want to automate the printing.
I have gotten as far VBA using a msgbox to ask user to define print area but not able to have it print the selected area?

This is what I have so far....help please.

Sub RangeSelectionPrompt()
Dim rng As Range
Set rng = Application.InputBox("Select a range", "Obtain Print Range", Type:=8)

Selection.PrintOut Copies:=1, Collate:=True
 
I have a excel log that I want to automate the printing.
I have gotten as far VBA using a msgbox to ask user to define print area but not able to have it print the selected area?

This is what I have so far....help please.

Sub RangeSelectionPrompt()
Dim rng As Range
Set rng = Application.InputBox("Select a range", "Obtain Print Range", Type:=8)

Selection.PrintOut Copies:=1, Collate:=True
Hello Bobpru,

You need to update the VBA code to set the print area to print the selected range. As Selection.PrintOut prints the currently selected range in the Excel sheet, not the range you are selecting using the InputBox.

Copy and paste the modified code that includes setting the print area and then it will print it.

Code:
Sub RangeSelectionPrompt()
    Dim rng As Range
    ' Prompt user to select a range
    Set rng = Application.InputBox("Select a range", "Obtain Print Range", Type:=8)
    ' Check if the user canceled the InputBox
    If rng Is Nothing Then
        MsgBox "No range selected. Operation canceled."
        Exit Sub
    End If
    ' Set the print area to the selected range
    With ActiveSheet
        .PageSetup.PrintArea = rng.Address
    End With
    ' Print the selected range
    rng.PrintOut Copies:=1, Collate:=True
End Sub

Download the Excel File:
 

Attachments

Online statistics

Members online
0
Guests online
7
Total visitors
7

Forum statistics

Threads
381
Messages
1,672
Members
721
Latest member
Dzaki wafi
Back
Top