I've been coding VBA for 20 years and this has me stumped. I'm testing the code below. If I step through it:
Sub TestSplitting_1()
Dim sColors As String
Dim aColors() As String
Dim sColor As Variant
sColors = "BLAK, FTGR, NAVY, REDD, WHIT"
aColors = Split(sColors, ",")
firstElement = aColors(0)
End Sub
... and hover over the variable firstElement after it's set, it shows no value. Not even empty double quotes, which it should have because it's a string.
If I change the first part of the string to say "apple" instead of "BLAK", so it looks like the code below, and I step through it:
Sub TestSplitting_2()
Dim sColors As String
Dim aColors() As String
Dim sColor As Variant
sColors = "apple, FTGR, NAVY, REDD, WHIT"
aColors = Split(sColors, ",")
firstElement = aColors(0)
End Sub
... and hover over the variable firstElement after it's set, it shows "apple".
What's the difference?? Is "BLAK" suddenly a reserved word, even when it's in a string?
(BTW I know it appears correctly in MsgBox firstElement. But I can't put the value into a cell, which is what I'm trying to do.)
EDIT:
I added a space after "BLAK" and before the comma. Now it works as expected! What the heck? I'll probably use that as a workaround, but I still want to understand why it doesn't work.




But I can't put the value into a cell- what happens instead?firstElementdeclared somewhere, and if Yes then how/where?