Multiple Apps

Convert array to delimited string

Ease of Use

Easy

Version tested with

2000, 2002, 2003 

Submitted by:

MOS MASTER

Description:

The function will convert a array to a delimited string. If no delimeter is given a "," is used as delimeter. 

Discussion:

You have a program function (Like mailmerge) or a self made function that requires a delimited string with a specific delimiter. This function returns such a string. For testing purposes a msgbox with the string is returned in the calling function. 

Code:

instructions for use

			

Option Explicit Sub TestFunction() Dim vTest As Variant vTest = Array("String", "Long", "Byte", "Integer", "Variant", "Boolean") MsgBox ArrayToDelimited(vTest, ";") End Sub ' \\ Function to convert a given array to a delimited string ' \\ If not specified the delimiter is "," Public Function ArrayToDelimited(vArray As Variant, _ Optional sDelim As String = ",") _ As String Dim sDelimString As String Dim lCounter As Long ' \\ Loop through array from the lower boud to the upper bound For lCounter = LBound(vArray) To UBound(vArray) ' \\ As long as lCounter is smaller then the upper bound of the array ' \\ add the value of that item to the string sDelimString and add delimiter sDelimString = sDelimString & _ CStr(vArray(lCounter)) & IIf(lCounter < UBound(vArray), sDelim, "") Next ' \\ Return delimited string ArrayToDelimited = sDelimString End Function

How to use:

  1. Open your document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
 

Test the code:

  1. From Word, press Alt + F8 to open the macro dialog box.
  2. Select TestFunction
  3. Click Run.
 

Sample File:

Convert array to delimited string.zip 7.77KB 

Approved by mdmackillop


This entry has been viewed 81 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express