Printing excel worksheets using VBA macro

srinimtech

New Member
Joined
Mar 29, 2017
Messages
3
Hei guys,

I have an excel workbook which contains worksheets, say, W1 to W9.
I recorded a macro to print the sheets and it is working fine to certain extent.

The code is like this:

Code:
[I]Sub Macro1()


[/I][I]    Sheets("W1").Select[/I]
[I]    ActiveWindow.SelectedSheets.PrintOut Copies:=3, Collate:=True, _[/I]
[I]    IgnorePrintAreas:=False[/I]


[I]    Sheets("W2").Select[/I]
[I]    ActiveWindow.SelectedSheets.PrintOut Copies:=3, Collate:=True, _[/I]
[I]    IgnorePrintAreas:=False[/I]

[I]        Sheets("W3").Select[/I]
[I]    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _[/I]
[I]    IgnorePrintAreas:=False[/I]


[I]    Sheets("W4").Select[/I]
[I]    ActiveWindow.SelectedSheets.PrintOut Copies:=3, Collate:=True, _[/I]
[I]    IgnorePrintAreas:=False[/I]
[I]
End Sub
[/I]
Question: In the above code, there's no option to define the printer to be used? Let's say, I need to save as pdf sometimes and sometimes it is a hard copy. How can I define a printer?

And, I'm stuck in the following 2 cases.

Case 1:

Say, for sheet W6, I have to select the paper source as "ManualTray". How can I define this?

Case 2:
I have to print EITHER W7 or W8 based on the below condition
If the value in cell A1 of sheet W7 < A2 of sheet W8, then W7 is to be printed. Else, print W8.
How can I achieve this?

I tried the below code:

-----------------------------------------------------------------------------------------------------------------------------------
Code:
Sub Macro2PDF_PA()


Dim w1 As Object
Set w1 = Worksheets("W7")
Set w2 = Worksheets("W8")


P1 = w1.Cells(1, 1)
P2 = w2.Cells(1, 1)
If P1 < P2 Then


    Sheets("W7").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False


Else
Sheets("W8").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
End If




End Sub
-----------------------------------------------------------------------------------------------------------------------------------
The code is printing sheet W8 only irrespective of the result of the condition.

I'm no expert in VBA. So, just copying code from recorded macros and tweaking it!!

Regards,
Srini.
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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