TOC2HTML

=TOC2HTML(reference, chapterKeyword, loopCounter)

reference
The range consists of multiple rows and a single column that contains the table of contents list.
chapterKeyword
The repeating keyword defines the main chapters to create the nested sections.
loopCounter
The counter to create the loop. Used as internally, so it is provided as zero or blank.

TOC2HTML creates the table of contents HTML code by transforming the provided list to an HTML list element to be used in the book's HTML page.

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
2,673
TOC2HTML creates the table of contents HTML code by transforming the provided list to an HTML list element to be used in the book's HTML page.

I usually create the HTML codes for the table of contents of the book pages to be published in the MrExcel Bookstore by using the list that @MrExcel sends me in a worksheet. Although it is possible to use regex101.com online tool with a few simple patterns, it takes a few steps to complete the transformation with Regex. Therefore, I wrote a user-defined function (UDF) in VBA to create the HTML code for the table of contents in a single step.

The following shows how I converted the UDF named TOC to a recursive Lambda function named TOC2HTML. You can find the UDF at the bottom of this post to compare the processes.

Excel Formula:
=LAMBDA(rng,chapterKeyword,i,
    LET(rows, ROWS(rng),
        IF(i = rows,
            "",
            LET(i, i+1,
                cll, INDEX(rng,i),
                isFirstLevel, IFERROR(FIND(chapterKeyword, cll),0),
                IF(isFirstLevel,
                     IF(i=1,
                    "<ul>","</ul></li>") & "<li><p>" & cll & "</p><ul>",
                    "<li>" & cll & "</li>"
                )
                & TOC2HTML(rng, chapterKeyword, i)
                & IF(i = rows - 1,
                        "</ul></li></ul>",
                        ""
                    )
            )
        )
    )
)
Toc.xlsm
AB
1HTML Code
2<ul><li><p>Chapter 0: Not an Introduction, But a Continuation</p><ul></ul></li><li><p>Chapter 1: Recap </p><ul><li>1.1: Best Practice Concept</li><li>1.2: Time Series Analysis</li><li>1.3: Financial Statement Theory</li><li>1.4: Control Accounts</li></ul></li><li><p>Chapter 2: What-If? Analysis</p><ul><li>2.1: Conditional Formulae</li><li>2.2: Offset</li><li>2.3: Scenario Analysis</li><li>2.4: Data Tables</li><li>2.5: Index and Match</li><li>2.6: Using Tornado Charts for Sensitivity Analysis</li><li>2.7: Simulations Analysis</li><li>2.8: Variance Analysis</li><li>2.9: Breakeven Analysis</li></ul></li><li><p>Chapter 3: Forecasting Considerations</p><ul><li>3.1: Seasonal / Cyclical Forecasting</li><li>3.2: Revising Forecasts</li><li>3.3: Pro-Rating Forecasts Over Time</li><li>3.4: Changing Periodicities</li><li>3.5: Modelling Historical, Actual and Forecast Data</li><li>3.6: Rolling Budgets and Charts</li><li>3.7: Forecasting Maximum Cash Required</li></ul></li><li><p>Chapter 4: Modelling Inventory</p><ul></ul></li><li><p>Chapter 5: Capital Expenditure</p><ul></ul></li><li><p>Chapter 6: Debt</p><ul><li>6.1: Debt Sculpting</li><li>6.2: Calculating Interest Rates Correctly</li><li>6.3: Useful Repayment Functions and Formulae</li></ul></li><li><p>Chapter 7: Valuation Considerations</p><ul><li>7.1: Deriving the Correct Cash Flow For DCF</li><li>7.2: Considering Discount Factors</li><li>7.3: Common DCF Modelling Errors</li><li>7.4: Using the Dividend Discount Model</li><li>7.5: Irrelevant IRR</li><li>7.6: Modified Internal Rate of Return (MIRR)</li><li>7.7: Smoothing Capital Expenditure</li><li>7.8: Calculating Economic Lives</li></ul></li><li><p>Chapter 8: Linking Models</p><ul></ul></li><li><p>Chapter 9: Life’s Too Short</p><ul><li>9.1: Section Numbering</li><li>9.2: US vs. European Dates</li><li>9.3: Sheet Referencing</li><li>9.4: Reducing File Size</li><li>9.5: Taking It to the Limit</li><li>9.6: Order of Operations</li><li>9.7: Keeping Styles Under Control</li><li>9.8: Wearing Protection</li></ul></li><li><p>Chapter 10: Look To The Future</p><ul><li>10.1: Dynamic Arrays</li><li>10.2: XLOOKUP and XMATCH</li><li>10.3: Let It Be</li><li>10.4: New Data Types</li></ul></li></ul>
3Chapter 0: Not an Introduction, But a Continuation
4Chapter 1: Recap
51.1: Best Practice Concept
61.2: Time Series Analysis
71.3: Financial Statement Theory
81.4: Control Accounts
9Chapter 2: What-If? Analysis
102.1: Conditional Formulae
112.2: Offset
122.3: Scenario Analysis
132.4: Data Tables
142.5: Index and Match
152.6: Using Tornado Charts for Sensitivity Analysis
162.7: Simulations Analysis
172.8: Variance Analysis
182.9: Breakeven Analysis
19Chapter 3: Forecasting Considerations
203.1: Seasonal / Cyclical Forecasting
213.2: Revising Forecasts
223.3: Pro-Rating Forecasts Over Time
233.4: Changing Periodicities
243.5: Modelling Historical, Actual and Forecast Data
253.6: Rolling Budgets and Charts
263.7: Forecasting Maximum Cash Required
27Chapter 4: Modelling Inventory
28Chapter 5: Capital Expenditure
29Chapter 6: Debt
306.1: Debt Sculpting
316.2: Calculating Interest Rates Correctly
326.3: Useful Repayment Functions and Formulae
33Chapter 7: Valuation Considerations
347.1: Deriving the Correct Cash Flow For DCF
357.2: Considering Discount Factors
367.3: Common DCF Modelling Errors
377.4: Using the Dividend Discount Model
387.5: Irrelevant IRR
397.6: Modified Internal Rate of Return (MIRR)
407.7: Smoothing Capital Expenditure
417.8: Calculating Economic Lives
42Chapter 8: Linking Models
43Chapter 9: Life’s Too Short
449.1: Section Numbering
459.2: US vs. European Dates
469.3: Sheet Referencing
479.4: Reducing File Size
489.5: Taking It to the Limit
499.6: Order of Operations
509.7: Keeping Styles Under Control
519.8: Wearing Protection
52Chapter 10: Look To The Future
5310.1: Dynamic Arrays
5410.2: XLOOKUP and XMATCH
5510.3: Let It Be
5610.4: New Data Types
Continuing Financial Modelling
Cell Formulas
RangeFormula
B2B2=TOC2HTML(toc_list,"Chapter ", )
Named Ranges
NameRefers ToCells
toc_list='Continuing Financial Modelling'!$A$3:$A$56B2


The following shows the result of the produced code in the browser.

1614967799003.png

HTML result


The VBA user-defined function. Although it is not necessary, I used indents and line breaks in the UDF version. It could be easily implemented in the Lambda version as well.
VBA Code:
Function TOC(rng As Range, firstLevel As String)
Dim cll As Range
Dim i As Integer
    For i = 1 To rng.Rows.Count
        Set cll = rng.Cells(i)
        If InStr(cll.Value, "Chapter ") Then
            If i = 1 Then
                TOC = "<ul>"
            Else
                TOC = TOC & vbCrLf & String(2, vbTab) & "</ul>" & vbCrLf & vbTab & "</li>"
            End If
            TOC = TOC & vbCrLf & vbTab & "<li>" & vbCrLf & String(2, vbTab) & "<p>" & cll.Value & "</p>" & vbCrLf & String(2, vbTab) & "<ul>"
        Else
            TOC = TOC & vbCrLf & String(3, vbTab) & "<li>" & cll.Value & "</li>"
        End If
        If i = rng.Rows.Count Then
            TOC = TOC & vbCrLf & String(2, vbTab) & "</ul>" & vbCrLf & vbTab & "</li>" & vbCrLf & "</ul>"
        End If
    Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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