VBA: Printing all but 2 sheets in a work book.

concretemonkey

Board Regular
Joined
Feb 5, 2004
Messages
95
Ok, firstly, i would like to say hello. :p

Secondly i would like to ask for your :rolleyes: !!

I would like to place a button on the first sheet of my work book that will allow my users to print all of the pages in the workbook.

I dont want them to be able to print the first page, and i dont want them to be able to print hidden pages. Also, every other sheet is a chart. I have been given help, but i didn't mention that some of the sheets were charts, so it only prints out the worksheets.

I would also like the printouts to be in order.

here is the code that I was given:

Code:
Sub PrintMost() 
    Dim data() As String, w As Worksheet, i As Integer 
    i = 0 
    For Each w In ActiveWorkbook.Worksheets 
        If w.Index <> 1 And w.Visible Then 
            i = i + 1 
            ReDim Preserve data(1 To i) 
            data(i) = w.Name 
        End If 
    Next w 
    If i > 0 Then Worksheets(data).PrintOut 
End Sub

from this i came up with the following code that will print all the sheets i want, but not in order:
Code:
Private Sub CommandButton1_Click() 
Dim data() As String, data2() As String, w As Worksheet, i As Integer, c As Chart, i2 As Integer 
i = 0 
i2 = 0 
For Each w In ActiveWorkbook.Worksheets 
If w.Index <> 1 And w.Visible Then 
i = i + 1 
ReDim Preserve data(1 To i) 
data(i) = w.Name 
End If 

Next w 

If i > 0 Then Worksheets(data).PrintOut 

For Each c In ActiveWorkbook.Charts 
If c.Index <> 1 And c.Visible Then 
i2 = i2 + 1 
ReDim Preserve data2(1 To i2) 
data2(i2) = c.Name 
End If 
Next c 

If i > 0 Then Charts(data2).PrintOut 

End Sub

I would really appreciate the help, as it is starting to aggrovate me.

I have tried everything that i know. Please help.

Thank you in advance,

Gareth.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can use the Sheets collection, which contains both Worksheets and Charts. Example:

Code:
Sub Test()
    Dim Sh
    For Each Sh In Sheets
        MsgBox Sh.Name
    Next Sh
End Sub
 
Upvote 0
You can try this:

Sub CommandButton1_Click()
Dim Sh
Dim i As Integer
i = 0
For Each Sh In Sheets
If Sh.Index <> 1 And Sh.Visible Then
i = i + 1
If i > 0 Then Sh.PrintOut
End If
Next Sh
End Sub

Elia
 
Upvote 0
emady said:
You can try this:

Sub CommandButton1_Click()
Dim Sh
Dim i As Integer
i = 0
For Each Sh In Sheets
If Sh.Index <> 1 And Sh.Visible Then
i = i + 1
If i > 0 Then Sh.PrintOut
End If
Next Sh
End Sub

Elia

OMG! Thank you ever so much.

After looking at the first code i was given, i set sh as Sheets, so the .inex wouldnt work.

That code works a treat. Thank you ever so much. I have been trying to sus this out since 8am this morning. My head hurts and i am due a coffee. thank you. thank you. thank you.

(y) - heres to you guys!!

:p time!!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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