Joining Variables Into A String With Delimiter
If your have a list of strings that need to be concatenated the simplest option is writing
v = var1 & "," & var2
If your list of variables is long then you may be tempted to write a function which has the following interface:
Function Concat(Delimiter As String, ParamArray vars() As Variant) As String
Smart use the functions Join and Array will make the implementation trivial (one line) to the extend that you may decide to abstain from making the Concat function and using the line directly. The idea is that Join already does what you want only that it requires an array. So you use the function Array to create it from the list of variables:
Concat = Join(Array(var1, var2), ",")
Obviously, this is only worth doing if you have a long list of variables (>=4):
Join (Array(var1, var2, var3, var4, var5, var6)
Click this to see the fragment to join multiple strings in action from Code VBA.
Join(sourcearray [, delimiter]) | |
sourcearray | Required. One-dimensional array containing substrings to be joined. |
delimiter | Optional. String character used to separate the substrings in the returned string. If omitted, the space character (" ") is used. If delimiter is a zero-length string (""), all items in the list are concatenated with no delimiters. |
Array(arglist)
arglist | The required argument is a comma-delimited list of values that are assigned to the elements of the array contained within the Variant. If no arguments are specified, an array of zero length is created. |