A VBA code to print all sheets except sheet1and sheet2

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
I want to put a command button on sheet named Data and provide it with code that print other sheets on the workbook except sheet Data and sheet next to it named Template. Thanks
I found this code here in the forum, it prints only one sheet other than the sheet which has the command button, also it hides the sheet after printing, i don't want that.

VBA Code:
Private Sub CommandButton2_Click()

Dim MyRange As Range
Dim ws As Worksheet
Dim Lastrow As Long
Dim i As Long
Dim n As Long
Dim j As Long
Dim PrintArea As String
Application.ScreenUpdating = False
Set ws = ActiveSheet
j = ActiveSheet.Index
Sheets(j + 1).Visible = True
Sheets(j + 1).Select
Debug.Print ws.Range("A1").Value
For i = 1 To 30840
If Cells(34 * i + 8, 1).Value <> "" Then
n = i + 1
Else
GoTo Printing
End If
Next i

Printing:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Print" & (j + 1) / 2 _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True

Sheets(j + 1).Visible = False
Sheets(j).Select
Application.ScreenUpdating = True

End Sub
 

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Doesn't for me if the names are correct. Post the full code as you have it now.
I tried your code, but when i press command button, it pritnt all sheets.

VBA Code:
Private Sub CommandButton2_Click()

Application.ScreenUpdating = False
Dim Wks As Worksheet

For Each Wks In ActiveWorkbook.Worksheets
If Trim(LCase(Wks.Name)) <> "data" And Trim(LCase(Wks.Name)) <> "template" Then
Wks.PrintOut
End If
Next Wks
Application.ScreenUpdating = True

End Sub
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Run the code below which will print to the Immediate Window then Copy and paste the results in the thread (copy and paste the results, do not type them manually).

VBA Code:
Sub dddd()
    Application.ScreenUpdating = False
    Dim Wks As Worksheet

    For Each Wks In ActiveWorkbook.Worksheets
        Debug.Print "| &"; Wks.Name; "& |", Len(Wks.Name)

    Next Wks
    Application.ScreenUpdating = True
End Sub
 

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Run the code below which will print to the Immediate Window then Copy and paste the results in the thread (copy and paste the results, do not type them manually).

VBA Code:
Sub dddd()
    Application.ScreenUpdating = False
    Dim Wks As Worksheet

    For Each Wks In ActiveWorkbook.Worksheets
        Debug.Print "| &"; Wks.Name; "& |", Len(Wks.Name)

    Next Wks
    Application.ScreenUpdating = True
End Sub
this code does not do anything,my friend
 

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Yes it does, it posts to the Immediate window (Ctrl +G in The VBE if you can't see it).
I don't know what this window is, plz send screenshots of your results, so i can know the place.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
1611876947980.png
 

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Run the code below which will print to the Immediate Window then Copy and paste the results in the thread (copy and paste the results, do not type them manually).

VBA Code:
Sub dddd()
    Application.ScreenUpdating = False
    Dim Wks As Worksheet

    For Each Wks In ActiveWorkbook.Worksheets
        Debug.Print "| &"; Wks.Name; "& |", Len(Wks.Name)

    Next Wks
    Application.ScreenUpdating = True
End Sub
I found the immediate window
immediate.png
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Now clear (you'll need to select the data and cut) the Immediate window and run the code below and paste those results.

VBA Code:
Sub cccc()
 Application.ScreenUpdating = False
    Dim Wks As Worksheet

    For Each Wks In ActiveWorkbook.Worksheets
        If Trim(LCase(Wks.Name)) <> "data" And Trim(LCase(Wks.Name)) <> "template" Then
           Debug.Print Wks.Name
        End If
    Next Wks
    Application.ScreenUpdating = True
End Sub
 

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Now clear (you'll need to select the data and cut) the Immediate window and run the code below and paste those results.

VBA Code:
Sub cccc()
Application.ScreenUpdating = False
    Dim Wks As Worksheet

    For Each Wks In ActiveWorkbook.Worksheets
        If Trim(LCase(Wks.Name)) <> "data" And Trim(LCase(Wks.Name)) <> "template" Then
           Debug.Print Wks.Name
        End If
    Next Wks
    Application.ScreenUpdating = True
End Sub
immediate2.png


What does it mean?
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
It means the code is ignoring the sheets called Template and Data when it runs the loop and so there is no reason why it would printout those 2 sheets in the code below.

VBA Code:
Sub PrintMe()
    Application.ScreenUpdating = False
    Dim Wks As Worksheet

    For Each Wks In ActiveWorkbook.Worksheets
        If Trim(LCase(Wks.Name)) <> "data" And Trim(LCase(Wks.Name)) <> "template" Then
           Wks.PrintOut
        End If
    Next Wks
    Application.ScreenUpdating = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,528
Messages
5,625,339
Members
416,096
Latest member
forevans

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
Top