THanks Norie,
avSheetsExport is a sheets array that is loaded a filtered range specialcells(xlvisible)
The full code is below
Thx
w
Option Explicit
Option Base 1
Sub ExportToPDF3()
'Excel evironment
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'Declare variables
Dim wb As Workbook
Dim ws As Worksheet
Dim wsControl As Worksheet
Dim wsWork As Worksheet
Dim wsWork2 As Worksheet
Dim lTabColor As Long
Dim i As Long, j As Long, k As Long, l As Long
Dim lRows As Long, lWorkRows As Long, lControlRows As Long
Dim rColorIndex As Range, rWork As Range, rCriteria As Range
Dim rControlTabName As Range, rControlColorIndex As Range
Dim avColorIndex() As Variant
Dim avCriteria() As Variant
Dim avSheetsExport() As Variant
Dim avSheets() As Variant
Dim sFilePrefix As String
Dim sFileSuffix As String
Dim sPath As String, sFileName As String, sExtension As String
'Object reference
Set wb = ThisWorkbook
Set wsControl = wb.Worksheets("Control")
Set wsWork = wb.Worksheets("Work") 'Color index
Set wsWork2 = wb.Worksheets("Work2")
Set rWork = wsWork.Range("A1")
'Intialize
i = 2
avSheets = Array("Control", "Work", "Work2")
sFilePrefix = InputBox("What is the file prefix?")
sFileSuffix = InputBox("What is the file suffix?")
sPath = "C:\Data\"
sExtension = ".pdf"
'Make sure autofilter is off
'Make sure all rows are visible
'Clear previous data from sheets that hold temporary data
For l = 1 To UBound(avSheets)
With wb
Set ws = .Worksheets(avSheets(l))
With ws
If .FilterMode Then
.ShowAllData
End If
.UsedRange.ClearContents
End With
End With
Next l
'List sheets and tab color index
With wb
For Each ws In .Worksheets
lTabColor = ws.Tab.ColorIndex
Select Case lTabColor
Case -4142
'Tab has no color
'Do nothing
Case Else
With wsControl
.Cells(i, 1).Value = ws.Name
.Cells(i, 2).Value = lTabColor
i = i + 1
End With
End Select
Next ws
End With
'Header Row
With wsControl
.[A1].Formula = "TabName"
.[B1].Formula = "TabColorIndex"
End With
'Generate unique list from color index list
With wsControl
lRows = .Cells(Rows.Count, 2).End(xlUp).Row
Set rColorIndex = .Range("B1:B" & lRows)
rColorIndex.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=rWork, _
Unique:=True
End With
'Load the color index data into an array
With wsWork
lWorkRows = .Cells(Rows.Count, 1).End(xlUp).Row
ReDim avColorIndex(1 To lWorkRows)
For j = 1 To UBound(avColorIndex)
avColorIndex(j) = .Cells(j, 1)
Next j
End With
'Loop through the color index array
For j = 2 To UBound(avColorIndex)
'Filter the color index range
avCriteria = Array(avColorIndex(1), avColorIndex(j))
Set rCriteria = wsWork.Range("IV1:IV2")
rCriteria = WorksheetFunction.Transpose(avCriteria)
'Range to filter
With wsControl
lControlRows = .Cells(Rows.Count, 1).End(xlUp).Row
Set rControlColorIndex = Range(.Cells(1, 1), .Cells(lControlRows, 2))
End With
'Filter the data
rControlColorIndex.AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=rCriteria, _
Unique:=False
rCriteria.ClearContents
'Load the visible range into a sheet array
With wsControl
Set rControlTabName = Range(.Cells(2, 1), .Cells(lControlRows, 1))
Debug.Print rControlTabName.Address
Set rControlTabName = rControlTabName.Rows.SpecialCells(xlCellTypeVisible)
Debug.Print rControlTabName.Address
ReDim avSheetsExport(1 To rControlTabName.Rows.Count)
Debug.Print rControlTabName.Rows.Count
avSheetsExport = rControlTabName
Debug.Print LBound(avSheetsExport)
Debug.Print UBound(avSheetsExport)
If .FilterMode = True Then
.ShowAllData
End If
End With
'Get the filename from the first sheet in the sheet array
Sheets(avSheetsExport(1)).Select
sFileName = ActiveSheet.Range("A1")
'Export the sheet array to pdf file
Sheets(avSheetsExport()).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFilePrefix & "_" & sFileName & "_" & sFileSuffix & sExtension, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next j
'Tidy up
'Erase arrays
Erase avColorIndex
Erase avSheets
Erase avSheetsExport
'Destroy objects
Set rControlColorIndex = Nothing
Set rControlTabName = Nothing
Set wsWork = Nothing
Set wsWork2 = Nothing
Set wsControl = Nothing
Set wb = Nothing
'Restore Excel environment
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Bookmarks