3

I'm trying to concatenate multiple strings and separate them by comma, and then subsequently to remove excess, leading and trailing commata.

For example, with an input of TEST("", "b", "c", "", ""), I would like to get b, c

However, my regex ,$| ,+|^, does not really take repeated commas into account:

Function TEST(a, b, c, d, e)
    res = a & ", " & b & ", " & c & ", " & d & ", " & e

    Debug.Print (res)
    Dim regex As Object, str, result As String
    Set regex = CreateObject("VBScript.RegExp")

    With regex
      .Pattern = ",$| ,+|^,"
    End With

    Dim ReplacePattern As String
    ReplacePattern = ""

    res = regex.Replace(res, ReplacePattern)

    TEST = res
End Function

How can I do this?

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
user1981275
  • 13,002
  • 8
  • 72
  • 101

2 Answers2

4

Most elegant is @ScottCraner's suggestion of TEXTJOIN (will remove this part of answer, if he wishes to post this as his own)

Private Function nonEmptyFields(ParamArray strings() As Variant) As String
    nonEmptyFields = WorksheetFunction.TextJoin(",", True, Array(strings))
End Function

enter image description here

Note: This will only work for Office 365+, but you can always create your own version of TEXTJOIN


Another option would be to loop over the ParamArray of strings and add them together, depending on their content (whether they are populated or empty)

Private Function nonEmptyFields(ParamArray strings() As Variant) As String

    Dim result As String
    Dim i As Byte

    For i = LBound(strings) To UBound(strings)
        If Len(strings(i)) <> 0 Then
            If result = vbNullString Then
                result = strings(i)
            Else
                result = result & "," & strings(i)
            End If
        End If
    Next i

    nonEmptyFields = result

End Function

Both would yield desired result with set up of

    Debug.Print nonEmptyFields(a, b, c, d, e, f) ' "", "b", "c", "", "", ""

enter image description here

Samuel Hulla
  • 6,617
  • 7
  • 36
  • 70
1

My ugly solution maintaining the same parameters:

Function TEST(a, b, c, d, e)

    If a <> "" Then res = a

    If b <> "" Then
        If res <> "" Then
            res = res & ", " & b
        Else
            res = b
        End If
    End If

    If c <> "" Then
        If res <> "" Then
            res = res & ", " & c
        Else
            res = c
        End If
    End If

    If d <> "" Then
        If res <> "" Then
            res = res & ", " & d
        Else
            res = d
        End If
    End If

    If e <> "" Then
        If res <> "" Then
            res = res & ", " & e
        Else
            res = e
        End If
    End If

    TEST = res

End Function

img1

dwirony
  • 5,487
  • 3
  • 21
  • 43
  • I feel like it would be a better idea to do this with dynamic amount of arguments (either the MS way of many `Optional` arguments or a `ParamArray`) – Samuel Hulla Apr 08 '19 at 16:35
  • Definitely - if I could loop through the parameters that'd be great, I'm just keeping it the way they had it setup initially. – dwirony Apr 08 '19 at 16:37
  • Technically you can, if you were to name the arguments in form of an ordered list (eg. `Optional ByVal string1 As String, Optional ByVal string2 As String, ...` then you can just loop over the variable names with a `for` loop and `IsMissing()`. But yeah, probably more hassle than it's worth, especially if all that can be solved with one simple `ParamArray` – Samuel Hulla Apr 08 '19 at 16:46