Print VBA

kyleboyd

New Member
Joined
Feb 22, 2011
Messages
15
I am trying to create a macro that will print a sheet based on a named cell in the sheet's contents. I have this:

Sub PrintSheets()
Dim Sh As Worksheet
Dim Printed
For Each Sh In ThisWorkbook.Worksheets
If Sh.CodeName <> "Sample" Then
If Sh.Range("Name") <> "" Then
Sh.PrintOut copies:=1
End If
End If
Next Sh
End Sub


Name is the named cell on each sheet.

But it returns this error:
method range of object _worksheet failed

I am a complete newbie to macro's, TIA!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe this?

Code:
Sub PrintSheets()
Dim Sh As Worksheet
For Each Sh In ActiveWindow.SelectedSheets
    If Sh.Name <> "Sample" Then
        Sh.PrintOut
    End If
Next Sh
End Sub
 
Upvote 0
Hi Peter!
Perhaps I was too vague...I think what yours does is just print the active sheet.

What I am trying to do is this:

I have about 40 some odd sheets each with a cell named "Name"

I want to create a Macro that will print the sheet if the Name cell is not blank. Actually come to think of it, I would like it to print if the Name cell is greater than today's date (the Name cell is a Date). Is that clearer?
 
Upvote 0
Ah, gotcha. Try

Code:
Sub PrintSheets()
Dim Sh As Worksheet
For Each Sh In ActiveWorkbook.Worksheets
    If Sh.Range("Name").Value > Date Then Sh.PrintOut
Next Sh
End Sub
 
Upvote 0
Great! I added

If Sh.Range("Name").Value > Date And Sh.Range("Name")<>"" Then Sh.PrintOut

to account for some cells that are blank (it was giving me an error with the blanks before)

Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,682
Members
452,937
Latest member
Bhg1984

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