Need VBA code to print specific pages or all pages in a MultiPage UserForm

pimp_mentality

New Member
Joined
Oct 15, 2015
Messages
46
Good day all,

As the topic suggested I would like a line of code which will allow me to print a specific page from a multipage UserForm or print the entire form (all pages) when click commandbutton I created

Assistance in this regard would be greatly appreciated.

I would also like a line of code which will clear all textboxes when I click the commandbutton I created.

Thanks
 
Add a listbox to the non-tab area which will be used to select pages to print with multiselect set to frmMultiSelectExtended
Add a command button to the non-tab area which will be used to print selected pages
Add the following code to the form code page, update control names in code to match yours

Code:
Option Explicit
Private Sub CommandButton1_Click()
    PrintSpecifiedFormTabs
End Sub

Private Sub UserForm_Activate()
    'PopulateListBox
    
    Dim lTabCount As Long
    Dim lIndex As Long
    
    lTabCount = Me.MultiPage1.Pages.Count
    For lIndex = 1 To lTabCount
        Me.ListBox1.AddItem lIndex
    Next
    
End Sub

Sub PrintSpecifiedFormTabs()

    Dim strThisWorkbook As String
    Dim lTabIndex As Long
    Dim iAnswer As VbMsgBoxResult
    Dim lPrintCount As Long

    strThisWorkbook = ActiveWorkbook.Name
    
    'How Many Selected?
    For lTabIndex = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(lTabIndex) Then
            lPrintCount = lPrintCount + 1
        End If
    Next
    
    If lPrintCount > 0 Then
        iAnswer = MsgBox("You have selected " & lPrintCount & " page" & _
            IIf(lPrintCount > 1, "s", "") & " to print." & vbLf & vbLf & _
            "Do you wish to continue?", vbYesNo, "Print Selected Page " & _
            IIf(lPrintCount > 1, "s", "") & "?")
        If iAnswer = vbYes Then
            For lTabIndex = 0 To UserForm1.ListBox1.ListCount - 1
                If UserForm1.ListBox1.Selected(lTabIndex) Then
                    Me.MultiPage1.Value = lTabIndex
                    'Debug.Print "print tab " & lTabIndex
                    Me.PrintForm
                End If
            Next
            
            'Deselect tabs
            For lTabIndex = 0 To Me.ListBox1.ListCount - 1
                If Me.ListBox1.Selected(lTabIndex) Then
                    Me.ListBox1.Selected(lTabIndex) = False
                End If
            Next
            
            MsgBox lPrintCount & " page" & IIf(lPrintCount > 1, "s", "") & _
                " sent to printer", vbInformation, "User Cancelled Printing"
        Else
            MsgBox "Printing Cancelled", vbExclamation, "User Cancelled Printing"
        End If
    End If
    
End Sub
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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