Sheets selection based on tab color

SoerenB

New Member
Joined
Nov 15, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
On basis on an old tread I have been able to build this code, but I do not want active sheet in the array, and when comenting that out, I get "out of range".
As I have found that is as no start of array has been set? How to solve that?

VBA Code:
Sub pdf_export()

Dim wsNames() As String
Dim ws As Worksheet

  ReDim wsNames(0)
  'wsNames(0) = ActiveSheet.Name

    For Each ws In ThisWorkbook.Sheets
        If ws.Tab.ColorIndex = 4 And ws.Visible = xlSheetVisible Then
            ReDim Preserve wsNames(UBound(wsNames) + 1)
            wsNames(UBound(wsNames)) = ws.Name
        End If
    Next ws
 
        Sheets(wsNames).Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:=Left(ThisWorkbook.FullName, (InStrRev(ThisWorkbook.FullName, ".", -1, vbTextCompare) - 1)) & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

But as I do not want Ac
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
On basis on an old tread I have been able to build this code, but I do not want active sheet in the array, and when comenting that out, I get "out of range".
As I have found that is as no start of array has been set? How to solve that?

VBA Code:
Sub pdf_export()

Dim wsNames() As String
Dim ws As Worksheet

  ReDim wsNames(0)
  'wsNames(0) = ActiveSheet.Name

    For Each ws In ThisWorkbook.Sheets
        If ws.Tab.ColorIndex = 4 And ws.Visible = xlSheetVisible Then
            ReDim Preserve wsNames(UBound(wsNames) + 1)
            wsNames(UBound(wsNames)) = ws.Name
        End If
    Next ws
 
        Sheets(wsNames).Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:=Left(ThisWorkbook.FullName, (InStrRev(ThisWorkbook.FullName, ".", -1, vbTextCompare) - 1)) & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

But as I do not want Ac

Does this work.

VBA Code:
Sub pdf_export()
Dim Ws As Worksheet
Dim strSheets As String

  For Each Ws In ThisWorkbook.Sheets
    If Ws.Tab.ColorIndex = 4 And Ws.Visible = xlSheetVisible And _
      (Ws.Name <> ActiveSheet.Name) Then
      strSheets = strSheets & Ws.Name & ","
    End If
  Next Ws
 
  Sheets(Split(Left(strSheets, Len(strSheets) - 1), ",")).Select
  
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
  Filename:=Left(ThisWorkbook.FullName, (InStrRev(ThisWorkbook.FullName, ".", -1, vbTextCompare) - 1)) & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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