print specific sheet and specific pages in worksheet based on cell data

dwanrizz

New Member
Joined
Feb 10, 2014
Messages
5
dJXxsfC
i have this to print specific worksheet using command button but i want to it can be set what pages will be printed base on cell data

dJXxsfC


dJXxsfC

Sub printsheet()
Dim ws As Worksheet, wks As Worksheet
Set wks = Worksheets("Main")

Select Case wks.Range("A1").Value
Case 1
Worksheets("My Sheet1").PrintOut
Case 2
Worksheets("My Sheet2").PrintOut
Case 3
Worksheets("my sheet3").PrintOut
End Select

dJXxsfC


End Sub

https://ibb.co/dJXxsfC - this the excel image sample.

anyone can help, thank you very much.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this

Code:
Sub printsheet()
    Dim ws As Worksheet, wks As Worksheet
    Dim pFrom As Long, pTo As Long
    
    Set wks = Worksheets("Main")
    pFrom = wks.Range("B1").Value
    pTo = wks.Range("C1").Value
    
    Select Case wks.Range("A1").Value
        Case 1
            Worksheets("My Sheet1").PrintOut from:=pFrom, To:=pTo
        Case 2
            Worksheets("My Sheet2").PrintOut from:=pFrom, To:=pTo
        Case 3
            Worksheets("my sheet3").PrintOut from:=pFrom, To:=pTo
    End Select
End Sub
 
Upvote 0
Try this

Code:
Sub printsheet()
    Dim ws As Worksheet, wks As Worksheet
    Dim pFrom As Long, pTo As Long
    
    Set wks = Worksheets("Main")
    pFrom = wks.Range("B1").Value
    pTo = wks.Range("C1").Value
    
    Select Case wks.Range("A1").Value
        Case 1
            Worksheets("My Sheet1").PrintOut from:=pFrom, To:=pTo
        Case 2
            Worksheets("My Sheet2").PrintOut from:=pFrom, To:=pTo
        Case 3
            Worksheets("my sheet3").PrintOut from:=pFrom, To:=pTo
    End Select
End Sub

Dear Mr. Dante thank you very much for your help.
it work's when the cell B1 and C1 is filled, but when i leave it empty or when i going to print one page only its go error any further help about this sir ?
 
Upvote 0
Dear Mr. Dante thank you very much for your help.
it work's when the cell B1 and C1 is filled, but when i leave it empty or when i going to print one page only its go error any further help about this sir ?

i mean leave it empty to print whole pages, is it possible ?
 
Upvote 0
Try this

Code:
Sub printsheet()
    Dim wks As Worksheet
    Dim sName As String
    Dim pFrom As Long, pTo As Long
    
    Set wks = Worksheets("Main")
    pFrom = Val(wks.Range("B1").Value)
    pTo = Val(wks.Range("C1").Value)
    
    Select Case wks.Range("A1").Value
        Case 1: sName = "My Sheet1"
        Case 2: sName = "My Sheet2"
        Case 3: sName = "My Sheet3"
    End Select
    If pFrom = 0 Or pTo = 0 Or pFrom > pTo Then
        Worksheets(sName).PrintOut
    Else
        Worksheets(sName).PrintOut from:=pFrom, To:=pTo
    End If
End Sub
 
Upvote 0
Try this

Code:
Sub printsheet()
    Dim wks As Worksheet
    Dim sName As String
    Dim pFrom As Long, pTo As Long
    
    Set wks = Worksheets("Main")
    pFrom = Val(wks.Range("B1").Value)
    pTo = Val(wks.Range("C1").Value)
    
    Select Case wks.Range("A1").Value
        Case 1: sName = "My Sheet1"
        Case 2: sName = "My Sheet2"
        Case 3: sName = "My Sheet3"
    End Select
    If pFrom = 0 Or pTo = 0 Or pFrom > pTo Then
        Worksheets(sName).PrintOut
    Else
        Worksheets(sName).PrintOut from:=pFrom, To:=pTo
    End If
End Sub
:cool: WOW thank you very much mr dante
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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