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
 
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
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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
 
Upvote 0
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
 
Upvote 0
1611876947980.png
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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