Results 1 to 3 of 3

Thread: [RESOLVED] generating a sequence of numbers from a specified value range (1-5 = 1,2,3,4,5)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    90

    Resolved [RESOLVED] generating a sequence of numbers from a specified value range (1-5 = 1,2,3,4,5)

    Hello Forum

    I would like a function that returns a string of values based on a range of numbers entered into a cell.

    For example, if a user has entered 1,2,3-9,14,17,24,21 then I would like a returned string of
    1,2,3,4,5,6,7,8,9,14,17,24,21

    The output should be in the order specified by the user rather than running lowest to highest.

    So the function has to look for '-' and then output the range between the starting and ending value.

    Thanks in advance for any assistance.

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: generating a sequence of numbers from a specified value range (1-5 = 1,2,3,4,5)

    I put a string of numbers (and/or ranges of numbers) in cell A1. This code will "parse" them and place them in column E, starting in row 1:

    Code:
    Sub numsOut()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim strOrig As String
        Dim strNums() As String
        Dim bounds() As String
        Dim upper As Integer
        Dim lower As Integer
        Dim myCount As Integer
        Dim i As Integer
        Dim j As Integer
        Dim nextRow As Integer
        
        Set wb = ActiveWorkbook
        Set ws = wb.Worksheets(1)
        strOrig = ws.Range("a1").Value
        strNums = Split(strOrig, ",")
        nextRow = 1
        For i = 0 To UBound(strNums)
            If InStr(strNums(i), "-") Then
                bounds = Split(strNums(i), "-")
                upper = bounds(1)
                lower = bounds(0)
                myCount = upper - lower + 1
                For j = 1 To myCount
                    ws.Range("e" & nextRow).Value = lower + j - 1
                    nextRow = nextRow + 1
                Next j
            Else
                ws.Range("e" & nextRow).Value = strNums(i)
                nextRow = nextRow + 1
            End If
        Next i
    End Sub
    NOTE: I've made some assumptions, ie. no extra characters, spaces, etc.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    90

    Re: generating a sequence of numbers from a specified value range (1-5 = 1,2,3,4,5)

    Many thanks. I turned it into a function and now it works exactly as I need it.

Posting Permissions

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



Click Here to Expand Forum to Full Width