Excel VBA Macro for automated paragraph numbering

mrwad

New Member
Joined
Oct 16, 2018
Messages
49
Can somebody help with macro for automated paragraph numbering in my document?

In column "A" I need to have paragraph numbers. I have three different types of numbering. They are "1", "1.1" and "1.11.1". In column "B" I have text and in column "C" I will insert corresponding text for macro to get information what number should be inserted in column "A". These options will be "main" for "1", "sub" for "1.1" and "sub-sub" for "1.11.1".

So here is an example of my document.

Code:
A           B                  C

1           Car parts        main
             Some text
             Some text
1.1        Wheels           sub
             Some text
1.2        Bumper          sub
             Some text
1.3        Windshield      sub
             Some text
1.4        Motor             sub
             Some text
1.4.1     Cylinders        sub-sub
             Some text
             Some text
             Some text
             Some text
1.4.2     Throttle          sub-sub
             Some text
             Some text
1.4.3     Spark plugs    sub-sub
             Some text
             Some text

2           Car options     main
             Some text
             Some text
             Some text
             Some text
             Some text
2.1        Leather seats  sub
             Some text
             Some text
2.2        Xenon-lights    sub
             Some text
2.3        Transmission    sub
             Some text
             Some text
             Some text
2.3.1      Robot             sub-sub
             Some text
             Some text
             Some text
2.3.2      Automatic       sub-sub
             Some text
             Some text

3           Other              main
             Some text
             Some text
3.1        Pas. car           sub

etc.
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about
Code:
Sub Addlevels()
   Dim m As Long, s As Long, ss As Long
   Dim Cl As Range
   
   For Each Cl In Range("C2", Range("C" & Rows.Count).End(xlUp))
      Select Case LCase(Cl.Value)
         Case "main"
            m = m + 1: s = 0: ss = 0
            Cl.Offset(, -2).Value = m
         Case "sub"
            s = s + 1: ss = 0
            Cl.Offset(, -2) = m & "." & s
         Case "sub-sub"
            ss = ss + 1
            Cl.Offset(, -2) = m & "." & s & "." & ss
      End Select
   Next Cl
End Sub
 
Upvote 0
How about
Code:
Sub Addlevels()
   Dim m As Long, s As Long, ss As Long
   Dim Cl As Range
   
   For Each Cl In Range("C2", Range("C" & Rows.Count).End(xlUp))
      Select Case LCase(Cl.Value)
         Case "main"
            m = m + 1: s = 0: ss = 0
            Cl.Offset(, -2).Value = m
         Case "sub"
            s = s + 1: ss = 0
            Cl.Offset(, -2) = m & "." & s
         Case "sub-sub"
            ss = ss + 1
            Cl.Offset(, -2) = m & "." & s & "." & ss
      End Select
   Next Cl
End Sub

Thanks a million!
 
Upvote 0

Forum statistics

Threads
1,223,718
Messages
6,174,077
Members
452,542
Latest member
Bricklin

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