Results 1 to 6 of 6
  1. #1
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142

    Range expanding algorithm

    Before I reinvent the wheel is anyone aware of some publicly available vba that will parse a range input string like "3-7" and return a list of numbers like "3,4,5,6,7". Or a little more advanced like "1,3-5,12" returns "1,3,4,5,12"

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Return the numbers where?

    I expect this will be original code for your requirement. It will involve common structures like loops and conditionals, maybe even an array.

    One solution based on the two data samples provided:
    Code:
    Function GetSeq(strIN As String) As String
    Dim aryS As Variant, strOut As String, x As Integer, y As Integer
    aryS = Split(strIN, ",")
    For x = 0 To UBound(aryS)
        If IsNumeric(aryS(x)) Then
            strOut = strOut & aryS(x) & ","
        ElseIf InStr(aryS(x), "-") > 0 Then
            For y = Split(aryS(x), "-")(0) To Split(aryS(x), "-")(1)
                strOut = strOut & y & ","
            Next
        End If
    Next
    GetSeq = strOut
    End Function
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    @June - modified your code slightly to remove the trailing comma, hope you don't mind

    Code:
    Function GetSeq(strIN As String) As String
    Dim aryS As Variant, strOut As String, x As Integer, y As Integer
    aryS = Split(strIN, ",")
    For x = 0 To UBound(aryS)
        If IsNumeric(aryS(x)) Then
            strOut = strOut & "," & aryS(x)
        ElseIf InStr(aryS(x), "-") > 0 Then
            For y = Split(aryS(x), "-")(0) To Split(aryS(x), "-")(1)
                strOut = strOut & "," & y
            Next
        End If
    Next
    GetSeq = Mid(strOut, 2)
    End Function

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    @June7, nice work!
    I was thinking of leaving it as is except for dealing with the trailing comma as
    strOut = Left(strOut, InStrRev(strOut, ",") - 1)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Yes, told myself not to forget that then guess what - forgot it. Thanks.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Thanks June!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. self-expanding cells
    By anakin74 in forum Reports
    Replies: 0
    Last Post: 01-31-2019, 12:26 PM
  2. Algorithm for program maintenance
    By Ricardobalbio in forum Queries
    Replies: 4
    Last Post: 05-21-2013, 01:32 PM
  3. Replies: 14
    Last Post: 02-08-2013, 03:06 PM
  4. algorithm for encoding password string
    By princess-1 in forum Security
    Replies: 2
    Last Post: 10-30-2010, 05:48 PM
  5. Creating an algorithm function.
    By NGLG in forum Access
    Replies: 0
    Last Post: 12-07-2008, 09:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums