Automate Table of Contents with 3 sections

rahildhody

Board Regular
Joined
Aug 4, 2016
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a macro that produces table of contents based on a section separator suffix that's defined in the ToC sheet.

It currently runs the macro on only 1 subsection: i.e. I can only go down to 1.1, 1.2 etc.

I would like this code to be modified to include a third subsection, again defined at the top as perhaps _SubSC that would perform the same function at the current code but for section 1.1.1, 1.1.2, 1.2.1 etc.

Any help on this matter would be greatly appreciated.

1679009591540.png


VBA Code:
Option Explicit

Sub ToC()
StartAppSettings

    Dim shtAct As Worksheet, shtToC As Worksheet
    Dim rngErrorCol As Range, rngSectSuff As Range, rngStartCell As Range
    Dim lngToCSection As Long, lngToC As Long, lngToCSub As Long
    Dim strText As String
    Dim calcMeth As XlCalculation
    Dim nSheets
    
    Set rngErrorCol = ThisWorkbook.Names("ToC_Err_Column").RefersToRange
    Set rngSectSuff = ThisWorkbook.Names("ToC_Section_Suffix").RefersToRange
    Set rngStartCell = ThisWorkbook.Names("ToC_Start_Cell").RefersToRange
    
'application level settings

    
    
    Set shtToC = Sheets("ToC")
    
'Placeholder sheets that do not need to be in ToC
    nSheets = Array("ToC", "DPD", "Tableau PnL", "Portfolio Mapping", "Opex Allocations", "Portfolio Import", "PnL Import", "Strat Cube Import", "Lending Fees Import", "FTE Import")
    
'check if ToC sheet exists; if not, create Toc sheet
    
    rngStartCell.Offset(2, 1).Resize(shtToC.UsedRange.Rows.Count - rngStartCell.Rows.Count + 2, 6).ClearContents
    
    On Error Resume Next
    rngStartCell.Offset(2, 1).Resize(shtToC.UsedRange.Rows.Count - rngStartCell.Rows.Count + 2).EntireRow.Ungroup
    On Error GoTo 0
    
'Add Error Check total label
    rngStartCell.Offset(2, 5).Value = "Errors"
    
    For Each shtAct In ActiveWorkbook.Worksheets
        If Not (IsNumeric(Application.Match(shtAct.Name, nSheets, 0))) Then 'shtAct.Name <> nSheets
            lngToC = lngToC + 1
            
                If InStr(shtAct.Name, rngSectSuff.Value) > 0 Then
                    
                    lngToCSection = lngToCSection + 1
                    lngToCSub = 0
                    
                    Call f_Hyperlink(rngStartCell.Offset(lngToC + 2, 1), shtAct, shtToC, CStr(lngToCSection))
                    Call f_Hyperlink(rngStartCell.Offset(lngToC + 2, 3), shtAct, shtToC, shtAct.Name)
                    
                ElseIf Not (IsNumeric(Application.Match(shtAct.Name, nSheets, 0))) Then 'shtAct.Name <> nSheets
                    
                    lngToCSub = lngToCSub + 1
                    
                    Call f_Hyperlink(rngStartCell.Offset(lngToC + 2, 2), shtAct, shtToC, CStr(lngToCSection & "." & lngToCSub))
                    Call f_Hyperlink(rngStartCell.Offset(lngToC + 2, 3), shtAct, shtToC, shtAct.Name)
                    
                    rngStartCell.Offset(lngToC + 2, 1).EntireRow.Group
                    rngStartCell.Offset(lngToC + 2, 3).IndentLevel = 3
                    
                    'Add error Checks
                    rngStartCell.Offset(lngToC + 2, 4).Formula = "=Sum('" & shtAct.Name & "'!" & rngErrorCol.Value & ":" & rngErrorCol.Value & ")"
                    If InStr(rngStartCell.Offset(lngToC + 2, 4).Value, "#") > 0 Then
                        rngStartCell.Offset(lngToC + 2, 4).Formula = "=Sum(" & shtAct.Name & "!A:A)"
                    End If
                    
                End If
        End If
        
    Next shtAct
    
'total errors
    rngStartCell.Offset(2, 4).Formula = "=Sum(" & rngStartCell.Offset(3, 4).Resize(lngToC + 1, 1).Address & ")"
    
'Add formatting
    rngStartCell.Offset(3, 4).Resize(lngToC + 2, 1).NumberFormat = "#,##0;[Red](#,##0);--"
    rngStartCell.Offset(3, 4).Resize(lngToC + 2, 1).HorizontalAlignment = xlCenter
    rngStartCell.Offset(3, 4).Resize(lngToC + 2, 1).VerticalAlignment = xlCenter
    
    rngStartCell.Offset(2, 1).Resize(lngToC + 2, 3).Font.Color = RGB(149, 79, 114)
    
    rngStartCell.Offset(2, 1).Resize(shtToC.UsedRange.Rows.Count - rngStartCell.Rows.Count + 2, 6).Font.Size = 8
    rngStartCell.Offset(2, 1).Resize(shtToC.UsedRange.Rows.Count - rngStartCell.Rows.Count + 2, 6).Font.Name = "Arial"
    
    Set rngErrorCol = Nothing
    Set rngSectSuff = Nothing
    Set rngStartCell = Nothing
    
'application level settings
EndAppSettings
    
End Sub

Function f_Hyperlink(rngToCPosition As Range, shtAct As Worksheet, shtToC As Worksheet, strText As String)

    shtToC.Hyperlinks.Add rngToCPosition, "", "'" & shtAct.Name & "'!$A$1", "Go to Sheet", strText

End Function
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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