VBA to print pages depending on option button

bademployee

Board Regular
Joined
Aug 19, 2010
Messages
184
Hi all,

Appreciate any help to make below work:

Code:
Private Sub CommandButton1_Click()
    Dim wks As Worksheet
    Set wks = Worksheets("FQF")
    With wks
        Select Case Frame1.ActiveControl.Name
            Case OptionButton1 'This is the name of the optionbutton
                .PageSetup.PrintArea = "$A$1:$Z$55"
                .LeftMargin = Application.InchesToPoints(0.25)
                .RightMargin = Application.InchesToPoints(0.25)
                .TopMargin = Application.InchesToPoints(0.5)
                .BottomMargin = Application.InchesToPoints(0.5)
                .HeaderMargin = Application.InchesToPoints(0.3)
                .FooterMargin = Application.InchesToPoints(0.3)
                .PaperSize = xlPaperA4
                .Orientation = xlLandscape
                .Zoom = 65
                ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True 'Print Preview
                'ActiveWindow.SelectedSheets.PrintOut Copies:=1 'Printout
            
            Case OptionButton2
                .PageSetup.PrintArea = "$A$1:$Z$183"
                .LeftMargin = Application.InchesToPoints(0.25)
                .RightMargin = Application.InchesToPoints(0.25)
                .TopMargin = Application.InchesToPoints(0.5)
                .BottomMargin = Application.InchesToPoints(0.5)
                .HeaderMargin = Application.InchesToPoints(0.3)
                .FooterMargin = Application.InchesToPoints(0.3)
                .PaperSize = xlPaperA4
                .Orientation = xlLandscape
                .Zoom = 65
                ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True 'Print Preview
                'ActiveWindow.SelectedSheets.PrintOut Copies:=1 'Printout
        End Select
    End With
End Sub

Currently, whatever option button is selected, the whole range(A1:Z183) is printing.

Thanks in advance

Mark
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
The name property is a string so it shuold read :
Code:
Case "OptionButton1"

However surely it is better to read the value?

Code:
    If OptionButton1.Value = True Then
        
    ElseIf OptionButton2.Value = True Then
    
    Else
        'neither option is selected
    End If
 
Upvote 0
The name property is a string so it shuold read :
Code:
Case "OptionButton1"

However surely it is better to read the value?

Code:
    If OptionButton1.Value = True Then
        
    ElseIf OptionButton2.Value = True Then
    
    Else
        'neither option is selected
    End If

You're right, I changed to:

Code:
Private Sub CommandButton1_Click()
With ActiveSheet
        If OptionButton2.Value = True Then
        .PageSetup.PrintArea = "$a$1:$z$55"
        .PageSetup.Orientation = xlLandscape
        .PageSetup.LeftMargin = Application.InchesToPoints(0.25)
        .PageSetup.RightMargin = Application.InchesToPoints(0.25)
        .PageSetup.TopMargin = Application.InchesToPoints(0.5)
        .PageSetup.BottomMargin = Application.InchesToPoints(0.5)
        .PageSetup.HeaderMargin = Application.InchesToPoints(0.3)
        .PageSetup.FooterMargin = Application.InchesToPoints(0.3)
        .PageSetup.Zoom = 65
        .PrintPreview


End If
         If OptionButton1.Value = True Then
        .PageSetup.PrintArea = "$a$1:$z$183"
        .PageSetup.Orientation = xlLandscape
        .PageSetup.LeftMargin = Application.InchesToPoints(0.25)
        .PageSetup.RightMargin = Application.InchesToPoints(0.25)
        .PageSetup.TopMargin = Application.InchesToPoints(0.5)
        .PageSetup.BottomMargin = Application.InchesToPoints(0.5)
        .PageSetup.HeaderMargin = Application.InchesToPoints(0.3)
        .PageSetup.FooterMargin = Application.InchesToPoints(0.3)
        .PageSetup.Zoom = 65
        .PrintPreview


End If
End With
End Sub

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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