+ Reply to Thread
Results 1 to 7 of 7

Sequential column numbering with reset

Hybrid View

  1. #1
    Registered User
    Join Date
    12-30-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    4

    Sequential column numbering with reset

    I have a column numbered so...
    1
    0
    0
    0
    1
    0
    1
    0
    0
    1
    1
    0
    0
    0
    0

    I would like the next column to number consecutively, starting at 1 each time a 1 is encountered, so my columns would be...
    1 1
    0 2
    0 3
    0 4
    1 1
    0 2
    1 1
    0 2
    0 3
    1 1
    1 1
    0 2
    0 3
    0 4
    0 5

    and so on.

    I'm at a loss to get this done. Any ideas?

    TIA

  2. #2
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Sequential column numbering with reset

    This should work assuming you have column headers in A1 and B1 and your binary list is in column A:

    Sub NumberedList()
    
        Dim i As Integer
        Dim k As Integer
        Dim cl As Range
        Dim preVal As Integer
        
        ActiveSheet.UsedRange
        
            i = 0
            k = 0
            With ActiveWorkbook.Worksheets
                With [a1]
                    For Each cl In Range(.Offset(1, 0), .End(xlDown))
                        If cl.Value <> "" Then
                            i = i + 1
                        End If
                    Next cl
                Do Until k = i
                    k = k + 1
                    If Range(.Offset(k, 0), .Offset(k, 0)).Value = 1 Then
                        Range(.Offset(k, 1), .Offset(k, 1)).Value = 1
                    Else
                        preVal = Range(.Offset(k - 1, 1), .Offset(k - 1, 1)).Value
                        Range(.Offset(k, 1), .Offset(k, 1)).Value = preVal + 1
                    End If
                Loop
                End With
            End With
    End Sub

  3. #3
    Registered User
    Join Date
    12-30-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    4

    Re: Sequential column numbering with reset

    Thank you, learning_vba! I can follow the code (I'm a software developer, but not VB). I appreciate that.

    But my problem now: I'm a real macro dummy in Excel. How do I get what you posted into a spreadsheet and execute it? I've never done that before. Either step-by-step, or point me to a good online help topic. I've been trying to find in Excel help to find something like "How to copy/paste a VBA macro into a spreadsheet" - I haven't found what I need.

    Thanks again for taking your time to help this macro newbie!

  4. #4
    Registered User
    Join Date
    12-30-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    4

    Re: Sequential column numbering with reset

    Got it into a vba form, and executed it. Now I'm getting a type mismatch (error 13) on this line:

    preVal = Range(.Offset(k - 1, 1), .Offset(k - 1, 1)).Value

    I have copy/pasted my column I'm working with into a new spreadsheet, into cell A1 (added a header to the column). Formatted the column as numeric, 0 decimals...

    Here are the first few rows...
    Rel RelPlus
    1
    0
    0
    1
    0
    0
    0
    0
    1
    0
    0

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sequential column numbering with reset

    How about just a formula?

    Row\Col
    A
    B
    C
    2
    1
    1
    B2: =IF(A2=1, 1, B1+1)
    3
    0
    2
    4
    0
    3
    5
    0
    4
    6
    1
    1
    7
    0
    2
    8
    1
    1
    9
    0
    2
    10
    0
    3
    11
    1
    1
    12
    1
    1
    13
    0
    2
    14
    0
    3
    15
    0
    4
    16
    0
    5
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    12-30-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    4

    Re: Sequential column numbering with reset

    Simplicity at its best! I love it...

    Your reputation just got a hit.

    Thanks, shg!!

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sequential column numbering with reset

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to add sequential numbering down a column until the next number 1 and start over
    By diaphanous1138 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2014, 04:52 PM
  2. [SOLVED] Sequential Numbering
    By Filtec in forum Excel General
    Replies: 7
    Last Post: 04-15-2013, 04:01 AM
  3. Sequential numbering
    By taylorackley in forum Excel General
    Replies: 2
    Last Post: 08-26-2010, 02:19 PM
  4. Sequential numbering
    By mojura in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-17-2009, 01:52 PM
  5. Sequential numbering
    By EH003268 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-12-2007, 09:47 AM

Tags for this Thread

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