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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Try

If trim(lower(Wks.Name)) <> "data" or trim(lower(Wks.Name)) <> "template" Then
 
Upvote 0
Try...
VBA Code:
If Trim(LCase(Wks.Name)) <> "data" And Trim(LCase(Wks.Name)) <> "template" Then
 
Upvote 0
Doesn't for me if the names are correct. Post the full code as you have it now.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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