Why won't split work for me?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,531
Office Version
  1. 365
Platform
  1. Windows
I copied this example from one of the M$FT pages, but when I try it, it gets a Run-time error "13": Type mismatch.

VBA Code:
Dim sPrioAskCols As String
sPrioAskCols = "1 4 7 10 13"
Dim PrioAskCols() As Variant
PrioAskCols = Split(sPrioAskCols)

What am I doing wrong?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I think it should be:
VBA Code:
Dim sPrioAskCols As String
sPrioAskCols = "1 4 7 10 13"
Dim PrioAskCols() As String
PrioAskCols = Split(sPrioAskCols)

Then Use CLng or CDbl to convert them to number
 
Upvote 0
I think it should be:
VBA Code:
Dim sPrioAskCols As String
sPrioAskCols = "1 4 7 10 13"
Dim PrioAskCols() As String
PrioAskCols = Split(sPrioAskCols)

Then Use CLng or CDbl to convert them to number
Yes, thanks. I found that on another page.
 
Upvote 0
Then Use CLng or CDbl to convert them to number
Do I really need to convert them? They seem to work without conversion:

VBA Code:
Const sPrioAskCols As String = " 1 4 7 10 13"
Dim PrioAskCols() As String
PrioAskCols = Split(sPrioAskCols)

?prioaskcols(1)
1
?prioaskcols(1)+5
 6

Anything wrong with that?
 
Upvote 0
I copied this example from one of the M$FT pages, but when I try it, it gets a Run-time error "13": Type mismatch.

VBA Code:
Dim sPrioAskCols As String
sPrioAskCols = "1 4 7 10 13"
Dim PrioAskCols() As Variant
PrioAskCols = Split(sPrioAskCols)

What am I doing wrong?
Your code will work fine if you simply remove the parentheses from the PrioAskCols declaration (regular Variants can hold anything including arrays).
VBA Code:
  Dim sPrioAskCols As String
  sPrioAskCols = "1 4 7 10 13"
  Dim PrioAskCols As Variant
  PrioAskCols = Split(sPrioAskCols)
 
Upvote 0
Solution
No. No problem. Sometimes we have. But in your case don't need.
OK, thanks. If I did want to convert the data to long integers, how would I do that? Would I need to define another array variable?
 
Upvote 0
Your code will work fine if you simply remove the parentheses from the PrioAskCols declaration (regular Variants can hold anything including arrays).
VBA Code:
  Dim sPrioAskCols As String
  sPrioAskCols = "1 4 7 10 13"
  Dim PrioAskCols As Variant
  PrioAskCols = Split(sPrioAskCols)
Thank you. That works. I now have three methods. Is any one of them preferable?
  1. Declare it as Variant (without the parens)? This is how I plan to go.
  2. Declare it as a String array?
  3. Declare it as a String array and then convert it to long integer?
 
Upvote 0
I don't think the method is all that important although my preference is for declaring the variable as Variant (like you plan to do)... that way, VB will use the value as needed (you will not have to convert it to Longs first). And as you noted earlier, VB will convert a string number to a real number when you use it in a calculation but I think doing that may require an extra internal step on VB's part.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top