Excel autonumber in column 1 based on level of indent in column 2

I have a spreadsheet with Column 2 containing tasks for a project. In column one, I would like to have an autonumber feature, (1,2,3,3.1,3.2,3.3,4.11,4.12, etc) which does the following:

1. Determine the level of indent on the cell adjacent right (column 2)
2. Increment based upon the cell above or if indent is "out-dented" incement from last indent of same level

Does this make sense? I am not sure if this is easy or massively difficult. Help me understand and I will go away if this is too hard.

A sample with a column 2 indents is attached.

Thank you!
JohnD
LVL 1
John DarbyPMAsked:
Who is Participating?
 
Martin LissOlder than dirtCommented:
You'll need to format column A as Text to get it to align.

Sub ColorByIndentLevel()
Dim lngLastRow As Long
Dim lngRow As Long
lngLastRow = Range("B1048576").End(xlUp).Row
Dim intIndent0 As Integer
Dim intIndent1 As Integer
Dim intIndent2 As Integer
Dim intIndent3 As Integer
Dim intIndent4 As Integer

For lngRow = 3 To lngLastRow ' To start with row 1, change the 3 to a 1)
    If Cells(lngRow, 2).Text <> "" Then
        With Cells(lngRow, 2)
            ' Other than black (case 0), the bigger the number (up to 255
            ' which is white) the lighter the shade of gray
            Select Case .IndentLevel
                Case 0
                    .Interior.Color = RGB(0, 0, 0)
                    .Font.Color = vbWhite
                    intIndent0 = intIndent0 + 1
                    Cells(lngRow, 1) = intIndent0
                    intIndent1 = 0
                    intIndent2 = 0
                    intIndent3 = 0
                    intIndent4 = 0
                Case 1
                    .Interior.Color = RGB(128, 128, 128)
                    intIndent1 = intIndent1 + 1
                    Cells(lngRow, 1) = intIndent0 & "." & intIndent1
                    intIndent2 = 0
                    intIndent3 = 0
                    intIndent4 = 0
                Case 2
                    .Interior.Color = RGB(169, 169, 169)
                    intIndent2 = intIndent2 + 1
                    Cells(lngRow, 1) = intIndent0 & "." & intIndent1 & "." & intIndent2
                    intIndent3 = 0
                    intIndent4 = 0
                Case 3
                    .Interior.Color = RGB(211, 211, 211)
                    intIndent3 = intIndent3 + 1
                    Cells(lngRow, 1) = intIndent0 & "." & intIndent1 & "." & intIndent2 & "." & intIndent3
                    intIndent4 = 0
                Case 4
                    .Interior.Color = RGB(220, 220, 220)
                    intIndent4 = intIndent4 + 1
                    Cells(lngRow, 1) = intIndent0 & "." & intIndent1 & "." & intIndent2 & "." & intIndent3 & "." & intIndent4
                Case Else
                    MsgBox "Add code for Case 5"
            End Select
        End With
    End If
Next

End Sub

Open in new window

0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

sounds like wrong tool for the job. Why don't you use Word instead? It's built-in and ready to go.

cheers, teylyn
0
 
Martin LissOlder than dirtCommented:
I'll modify the workbook I created for you.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
John DarbyPMAuthor Commented:
Dang Martin, that rocks! It also is teaching me how to do it, since I have 3 more levels of indent to create (Cases 5-7).

Thank you Professor! :)
0
 
John DarbyPMAuthor Commented:
Marty is the master of Excel and VBA! Thank you, my friend!
0
 
Martin LissOlder than dirtCommented:
Thank you for the kind words and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.