Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:44:45+00:00 2026-05-15T03:44:45+00:00

I want to implement a VBA function to number Excel rows based upon the

  • 0

I want to implement a VBA function to number Excel rows based upon the grouping depth of the row.

But I think a general algorithm for generating TOCs is more interesting.

The problem is:

Given a list of “indented” lines such as

One
 Two
  Three
   Four
 Five
Six

(the “indentation level” may be assumed to be known and part of the input data)

To generate the following output:

1.    One
1.1    Two
1.1.1   Three
1.1.1.1  Four
1.2    Five
2.    Six

Of course my code is up and running … and also hidden under THWoS (The Heavy Weight of Shame)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:44:46+00:00Added an answer on May 15, 2026 at 3:44 am

    Use a stack for the numbers. Loop through each row, and check the indentation level of each row, with no indentation being level 1.

    1. If the current indentation level is greater than the size of the stack push as many ones as the difference is onto the stack (the difference would usually be just one, but this works even if someone puts a level 3 heading under a level 1 heading, for instance)
    2. If the current indentation level is less than the size of the stack, pop and discard as many numbers as the difference is and then increment the top number on the stack.
    3. If the current indentation level is equal to the size of the stack, increment the top number on the stack

    For each row, the current title number is the numbers on the stack concatenated together with a . to separate them.

    Note how the size of the stack handily represents the previous line’s indentation level.

    For people who find it easier to read code, here’s a JavaScript implementation for modern browsers:

    const toc = `
    One
     Two
      Three
       Four
     Five
      Six
      Seven
     Eight
    Nine
    Ten
    `;
    
    let stack = [];
    
    toc.trim().split(/\n/g).forEach(line => {
      // Gets the identitation level with 1 being no indentation and so forth
      let level = line.match(/^\s*/)[0].length + 1;
    
      if (level > stack.length) {
        while (level > stack.length)
          stack.push(1);
      } else {
        while (level < stack.length)
          stack.pop();
    
        stack[stack.length - 1]++;
      }
      
      let title = stack.join(".") + ". " + line.trim();
    
      document.body.appendChild(document.createElement("div")).innerText = title;
    });
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 472k
  • Answers 472k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970]; // NSTimeInterval is defined… May 16, 2026 at 3:43 am
  • Editorial Team
    Editorial Team added an answer It looks like JavaConversions does the job somewhat: import scala.collection.JavaConversions._ May 16, 2026 at 3:43 am
  • Editorial Team
    Editorial Team added an answer Every view 'should' have a specific model. This is sometimes… May 16, 2026 at 3:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.