Results 1 to 7 of 7

WBS Function

Threaded View

  1. #1
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    WBS Function

    This function produces a Work Breakdown Schedule (WBS)-like numbering system such as:

    1
    1.1
    1.2
    1.2.1
    2
    2.1
    2.1.1

    The function can start in any cell, and the first occurrence of the function is automatically assigned the value "1." Subsequent occurrences of the function in the same column will increment the counter or level depending on the indent level of the cell immediately to the right. No indent is N, indent 1 is N.N, Indent 2 is N.N.N, etc. You can have as many indents as needed. Just copy the formula down for as many tasks as you have and indent the tasks accordingly.

    I could probably do a bit more cleanup of the code, but I'm lazy.

    Function WBS() As String
    Dim MyCell As String            ' The address of the cell calling the function
    Dim MyRow As Long               ' Row of the function
    Dim MyCol As Long               ' Column of the function
    Dim MyLevel As Long             ' Level of the cell to the right of the function
    Dim UpLevel As Long             ' Level of the cell one up and to the right of the function
    
    Dim Firstrow As Long            ' First row with this formula
    Dim i As Long                   ' Row index
    Dim rng As Range                ' For find command
    
    Dim PrevVal As String           ' Value of the previous level
    Dim PrevArr() As String         ' Array holding the parts
    Dim NewVal As String            ' New value of string
    
    Application.Volatile
    
    ' Initalize variables
    MyCell = ThisCell
    
    MyRow = Range(MyCell).Row
    MyCol = Range(MyCell).Column
    MyLevel = Range(MyCell).Offset(0, 1).IndentLevel
    UpLevel = Range(MyCell).Offset(-1, 1).IndentLevel
    PrevVal = Range(MyCell).Offset(-1, 0).Value
    
    ' Is this the first row?
    Set rng = Range(Cells(1, MyCol), Cells(MyRow, MyCol)).Find("WBS", LookIn:=xlFormulas, lookat:=xlPart)
    If rng.Row = MyRow Then
        WBS = 1
        Exit Function
    End If
    
    ' Otherwise remember the first row.
    Firstrow = rng.Row
    
    'Compare the level of the previous task with the current task
    If UpLevel = MyLevel Then
        ' Add one to the last digit
        PrevArr = Split(PrevVal, ".")
        PrevArr(UBound(PrevArr)) = PrevArr(UBound(PrevArr)) + 1
        WBS = Join(PrevArr, ".")
    ElseIf UpLevel < MyLevel Then
        ' Append .1 to the string
        WBS = PrevVal & ".1"
    ElseIf UpLevel > MyLevel Then
        ' Find previous task with that level and add one to last digit
        For i = MyRow - 1 To Firstrow Step -1
            If Cells(i, MyCol).Offset(0, 1).IndentLevel = MyLevel Then
                PrevVal = Cells(i, MyCol)
                Exit For
            End If
        Next i
        PrevArr = Split(PrevVal, ".")
        PrevArr(UBound(PrevArr)) = PrevArr(UBound(PrevArr)) + 1
        WBS = Join(PrevArr, ".")
    Else
        ' We should never get here
        WBS = "???"
    End If
    
    End Function
    
    Function ThisCell() As String
        ThisCell = Application.Caller.Worksheet.Cells(Application.Caller.Row, Application.Caller.Column).Address
    End Function
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. COUNTUNIQUE Function in Google Sheets; Excel lacks a direct counterpart to this function?
    By PivotTablePSHomage in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-18-2018, 05:27 AM
  2. Calling function inside function. (aka nested function)
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 05:58 AM
  3. Replies: 1
    Last Post: 06-10-2015, 12:56 PM
  4. Replies: 13
    Last Post: 04-08-2014, 05:46 AM
  5. Replies: 2
    Last Post: 01-15-2014, 11:40 PM
  6. [SOLVED] IF Function referencing IsNumber, Match, Left function on separate sheets
    By Touch9713 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-12-2013, 10:09 PM
  7. Replies: 1
    Last Post: 03-21-2012, 11:22 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1