Consulting

Results 1 to 7 of 7

Thread: Array to line list

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location

    Array to line list

    I'm trying create a written out list from a delimited list.

    For example "Apple" "Apple|Peach" "Apple|Peach|Pear" will be written out as:

    Apple
    Apple and Peach
    Apple, Peach and Pear

    A few of you here can usually do in 5 lines of code what takes me ten. Any suggestion to streamline the following:

    [Option Explicit
    Sub Demo()
      MsgBox fcnLineList("Apple")
      MsgBox fcnLineList("Apple|Peach")
      MsgBox fcnLineList("Apple|Peach|Pear")
      MsgBox fcnLineList("Apple|Peach|Pear|Plum")
      MsgBox fcnLineList("Apple|Peach|...|...|...|Pear|Plum")
    End Sub
    Function fcnLineList(ByRef strIn As String) As String
    Dim arrIn() As String
    Dim lngIndex As Long
      If Right(strIn, 1) = "|" Then strIn = Left(strIn, Len(strIn) - 1)
      arrIn = Split(strIn, "|")
      Select Case UBound(arrIn)
        Case 0
          fcnLineList = arrIn(0)
        Case 1
          fcnLineList = arrIn(0) & " and " & arrIn(1)
        Case Else
          For lngIndex = 0 To UBound(arrIn) - 2
            fcnLineList = fcnLineList & arrIn(lngIndex) & ", "
          Next lngIndex
          fcnLineList = fcnLineList & arrIn(UBound(arrIn) - 1) & " and " & arrIn(UBound(arrIn))
      End Select
    End Function
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Greg,

    You could omit the Case 1 processing.

    Here's an alternative function:
    Function fcnLineList(ByRef strIn As String) As String
    Dim StrTmp As String
    If Right(strIn, 1) = "|" Then strIn = Left(strIn, Len(strIn) - 1)
    Select Case UBound(Split(strIn, "|"))
      Case 0: fcnLineList = strIn
      Case Else
        StrTmp = Left(strIn, InStrRev(strIn, "|") - 1)
        fcnLineList = Replace(StrTmp, "|", ", ") & " and " & Split(strIn, "|")(UBound(Split(strIn, "|")))
    End Select
    End Function
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Paul,

    Very nice. Thanks!
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Variant on the theme, not using an array, but just using replace...
    [VBA]
    'convert pipe-delimited string to comma with and as last item
    Public Function fGiveMePhrase(ByVal sTest As String) As String
    Dim i As Integer

    'not sure if you need this...
    If Right(sTest, 1) = "|" Then
    sTest = Left(sTest, Len(sTest) - 1)
    End If
    sTest = Replace(sTest, "|", ", ")
    'get position of last comma
    i = InStrRev(sTest, ", ")
    'and replace it with " and ", preserving the first part of the string
    If i > 0 Then
    sTest = Left(sTest, i - 1) & Replace(sTest, ", ", " and ", i, 1)
    End If
    fGiveMePhrase = sTest
    End Function
    [/VBA]

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Thanks Jason! Nice as well.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Another one

    Function fcnLineList(ByRef strIn As String) As String
        If Right(strIn, 1) = "|" Then strIn = Left(strIn, Len(strIn) - 1)
        fcnLineList = Replace(Replace(strIn, "|", ", ", 1, Len(strIn) - Len(Replace(strIn, "|", "")) - 1), "|", " and ")
    End Function
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    xld,

    Typically my skull starts to crack with the first nested Replace method but nice nonetheless.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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