0 votes
in VBA by Beginner (10 points)

The following code displays the items of the array one after the other:

Sub DynamicArray2()
    Dim arr() As String
        arr = Split("Apple,Banana,Cherry,Date,Elderberry", ",") '   "," forces the MsgBox to display one item after the other
        
    Dim arrCount As Integer
    
    For arrCount = LBound(arr) To UBound(arr)
        MsgBox arr(arrCount)
    Next arrCount
End Sub

The following code displays the items in one string by separating the items with a comma:

Sub DynamicArray2()
    Dim arr() As String
        arr = Split("Apple,Banana,Cherry,Date,Elderberry", ", ") 'take notice of the space after the comma
        
    Dim arrCount As Integer
    
    For arrCount = LBound(arr) To UBound(arr)
        MsgBox arr(arrCount)
    Next arrCount
End Sub

Any explanation for this?

Many thanks!

1 Answer

+2 votes
by Skilled (466 points)
selected by
 
Best answer

Here is the syntax for Split:

Split(expression, [ delimiter, [ limit, [ compare ]]])

In the first case, 

delimiter = ","

which separates the words in the first argument expression. In the second case, 

delimiter = ", "

which does not exist in expression, so Split returns expression.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...