McMillan's Visual Basic Code - Code Snippets - CombineText Function
Code Snippets
Home



Combine Text using the ParamArray Keyword

This is a very useful code snippet for combining text with a delimiter. A good example is taking a street, suburb and city and combining them with a comma between them (ie. Street, Suburb, City - and if the suburb's empty then it will return Street, City).

Using a ParamArray makes the function flexible (and reusable) because there's no limit to the amount of text you can combine at once.

Option Explicit

Private Function CombineText(Delimeter As String, ParamArray Text()) As String

   Dim v As Variant
   Dim s As String

   For Each v In Text
      If Len(s) = 0 Then
         s = v
      ElseIf Len(v) > 0 Then
         s = s & Delimeter & v
      End If
   Next

   CombineText = s

End Function

'Usage

Dim Address as String

Address = CombineText(", ", "123 Some Street", "Your Suburb", "That City")

© Copyright Notice

Unless otherwise stated, the code on this site is Copyright to Andrew McMillan. You may use this code in your projects (both commercial and non-commercial) but you are not permitted to republish this code in any form without the Author's prior consent.

The code on this site is supplied "as is" and no claims are made as to its soundness. The Author claims no responsibility for or liability from use of said source code.


Home