Share via

Multi level numbering VBA function help

Anonymous
2019-02-14T21:56:33+00:00

Hello,

There's an example here of a function to create multi level numbering:

https://www.ozgrid.com/forum/forum/help-forums/excel-general/74617-automatic-dynamic-row-numbering-for-outline-numbers-like

It's exactly what I need except it only works for 3 levels. I have 5 levels. Is there anyone out there who knows of any examples of how I can achieve 5 levels of number (I like the idea of a function because I want the numbering to be dynamic.

Thanks in advance,

Mark

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2019-03-05T16:24:18+00:00

Hi,

Here is an answer for a similar problem few years ago.

Data in 5 columns (5 levels) col 2-col 6

Results in column 1 (in row 1 are headers)

pic1

result

pic2

vba macro

Sub Indexing()

'### by Rick Rothstein ###

  Dim X As Long, Z As Long, LastCol As Long, LastRow As Long

  Dim OutStr As String, Arr() As String

  Const StartCol As Long = 2

  Const StartRow As Long = 2

  Const LastUsedCol As Long = 6

  LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, _

            SearchDirection:=xlPrevious, LookIn:=xlValues).Row

  'LastUsedCol = Cells.Find(What:="*", SearchOrder:=xlByColumns, _

                SearchDirection:=xlPrevious, LookIn:=xlValues).Column

  ReDim Arr(1 To LastUsedCol)

  For X = StartRow To LastRow

    LastCol = Cells(X, Columns.Count).End(xlToLeft).Column

    For Z = StartCol To LastUsedCol

      If Z > LastCol Then

        Arr(Z) = ""

      ElseIf Len(Cells(X, Z).Value) Then

        Arr(Z) = Val(Arr(Z)) + 1

      Else

        Arr(Z) = Val(Arr(Z))

      End If

      Cells(X, StartCol).Offset(, -1).Value = Replace(Trim(Join(Arr)), " ", " .")

    Next

  Next

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2019-03-06T20:29:32+00:00

    Many thanks to you (and Rick), that's exactly what I needed.

    Cheers,

    Mark

    Was this answer helpful?

    0 comments No comments