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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Sanjeev1976

Active Member
Joined
Dec 25, 2008
Messages
268
Try the below code :

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Dim Wks As Worksheet

For Each Wks In ActiveWorkbook.Worksheets
If Wks.Name <> "Data" or Wks.Name <> "Template" Then
Wks.PrintOut
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
Try the below code :

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Dim Wks As Worksheet

For Each Wks In ActiveWorkbook.Worksheets
If Wks.Name <> "Data" or Wks.Name <> "Template" Then
Wks.PrintOut
End If
Next Wks
Application.ScreenUpdating = True
End Sub
The code print all sheets.
 

Sanjeev1976

Active Member
Joined
Dec 25, 2008
Messages
268
Just check the sheet names i.e. Data and Template and correct the code with the correct names in the line

If Wks.Name <> "Data" or Wks.Name <> "Template" Then
 

seragrefaat

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

ADVERTISEMENT

Just check the sheet names i.e. Data and Template and correct the code with the correct names in the line

If Wks.Name <> "Data" or Wks.Name <> "Template" Then
The names are correct still printing all sheets
 

Sanjeev1976

Active Member
Joined
Dec 25, 2008
Messages
268
Try

If trim(lower(Wks.Name)) <> "data" or trim(lower(Wks.Name)) <> "template" Then
 

seragrefaat

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

ADVERTISEMENT

Try

If trim(lower(Wks.Name)) <> "data" or trim(lower(Wks.Name)) <> "template" Then
It is not working either, it says (sub or function not defined) and highlighting on lower in the code
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try...
VBA Code:
If Trim(LCase(Wks.Name)) <> "data" And Trim(LCase(Wks.Name)) <> "template" Then
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Doesn't for me if the names are correct. Post the full code as you have it now.
 

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