PDA

View Full Version : [SOLVED] userform input box to print a page



selfteaching
03-09-2019, 04:53 PM
Hi

I set up a spreadsheet with data and print numbers.

I have a Userform that will print certain page numbers

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Sheets("Sheet1").Select
Select Case Me.ListBox1.Value
Case Is = "A": ActiveSheet.PrintOut From:=4, To:=6, Copies:=1
Case Is = "B": ActiveSheet.PrintOut From:=7, To:=9, Copies:=1
Case Is = "C": ActiveSheet.PrintOut From:=10, To:=11, Copies:=1
Case Is = "D": ActiveSheet.PrintOut From:=13, To:=15, Copies:=1
Case Is = "E": ActiveSheet.PrintOut From:=16, To:=18, Copies:=1
Case Is = "F": ActiveSheet.PrintOut From:=19, To:=21, Copies:=1
Case Is = "G": ActiveSheet.PrintOut From:=22, To:=23, Copies:=1
........
I'm trying to add an input (text) box to be able to print only a particular number
the user would put in the number that is needed to print.
The way it works now is if ,as an example, the user needs printout >> 5, 4,5,and 6 gets printed

I thought about making 23 list boxes and adapting the above code, but would like to make it easier for me, and learn somethi


mike

Logit
03-09-2019, 07:08 PM
.
The following macro will show the Windows PrintDialog form :



Option Explicit


Sub commandbutton1_click()
Application.Dialogs(xlDialogPrint).Show
End Sub

gmayor
03-09-2019, 09:56 PM
You could make the list box multi-select and have as many entries in the list as you have pages, then select the pages you want to print and loop through the items in the list box and print only those that are selected e.g. as follows. Note that the first item in a list box is indexed 0 so to print the item associated with a selected list item add 1 to the index.


Dim iLst As Integer
For iLst = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(iLst) = True Then
'do stuff with the selected item e.g
ActiveSheet.PrintOut from:=iLst + 1, to:=iLst + 1, copies:=1
End If
Next iLst

selfteaching
03-10-2019, 09:55 AM
Hi Logit, gmayor,

I like them both. Great learning tools.

Thank you both for the help.

Mike. :beerchug: :beerchug:

Logit
03-10-2019, 10:31 AM
You are welcome.