Macro to Collapse columns on all worksheets between two tabs and then save as pdf

MarkB5432

New Member
Joined
Jul 3, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have an excel workbook with around 50 tabs sandwiched in between a tab labelled 'start' and a tab labelled 'end'. The 50 tabs combined produce a single 50+ page report when saved as a pdf.

Within each of the 50 tabs there are rows/columns that I want to make sure are collapsed (grouped) prior to saving.

I basically need a single Excel macro that performs two actions:


Step 1
Group/Collapse relevant columns and rows between the 'start' and 'end' tabs

I've found the following VBA code below that works but only applies to a single active worksheet … not sure how to apply this to the workbook or specifically between the 'start' and 'end' tabs

Sub Collapse_All()
ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
End Sub


Step 2
Select all the sheets between the 'Start' and 'End' tab and save as a single pdf file (this will be saved in the same folder location as the excel file)


I'm no good at writing macro's whatsoever, so I'm somebody could provide me with the code for a single macro that I can simply copy and paste it would be very much appreciated!!

Thank you in advance!!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
This should apply the code to all sheets except "start" and "end"
VBA Code:
Sub Collapse_All()
For Each sh In ThisWorkbook.Sheets   'assumes code will run from same workbook.
    If sh.name <> "start" And sh.Name <> "end" Then
       ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
    End If
Next
End Sub
 
Upvote 0
Thank for your reply @JLGWhiz , Unfortunately that VBA only appears to be working for the active sheet, and not the sheets between the 'start' and 'end' tabs
 
Upvote 0
Thank for your reply @JLGWhiz , Unfortunately that VBA only appears to be working for the active sheet, and not the sheets between the 'start' and 'end' tabs
My goof, change the line with ActiveSheet to:

VBA Code:
sh.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
 
Upvote 0
Thanks! - Just tested the VBA and it collapsed all the grouped columns in the workbook.

Is there any way of restricted the VBA to only group those sheets that fall between the 'start' and 'end' tabs, and not to whole workbook? (e.g. any tabs placed before and including the 'start' tab are not captured by the VBA)?
 
Upvote 0
Try this for both steps.
VBA Code:
Public Sub Save_Sheets_As_PDF()

    Dim currentSheet As Worksheet
    Dim replaceFlag As Boolean
    Dim i As Long
    
    With ThisWorkbook
        replaceFlag = True
        Set currentSheet = .ActiveSheet
        For i = .Worksheets("Start").Index + 1 To .Worksheets("End").Index - 1
            .Worksheets(i).Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
            .Worksheets(i).Select replaceFlag
            replaceFlag = False
        Next
        .ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Replace(.FullName, ".xlsm", ".pdf"), _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        currentSheet.Select
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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