Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split().....giving me an error!!! 2

Status
Not open for further replies.

qajussi

Programmer
Mar 22, 2004
236
0
0
US
Dim strText
strText = "MyHello.txt"
Dim strItem()
Dim j
strItem = Split(strText, ".", -1)
For j = 0 To strItem.GetUpperBound(0)
MsgBox(strItem(j))
Next

Type mismatch at split line
************************
or
Dim strText
strText = "MyHello txt"
Dim strItem()
Dim j
strItem = strText.Split(".")
For j = 0 To strItem.GetUpperBound(0)
MsgBox(strItem(j))
Next

this one tells me I need Object..

**********************Hi!
This should be a very simple VBS code.
But it is giving me an error..
I am getting errors..
Can you help??
 
Try:
Code:
Dim strText
strText = "MyHello txt"
Dim strItem()
Dim j 
strItem = strText.Split(".")
For j = 0 To [COLOR=red yellow]UBound(strItem)[/color red yellow]
 MsgBox(strItem(j))
Next

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Thank you.

But I am still getting an error.
Error: Object required : 'MyHello txt'
VBS runtime error

I copied your code and ran it ...at line 5
strItem = strText.Split(".")


 
Sorry, didn't catch that one. Try this:
Code:
Dim strText
strText = "MyHello txt"
Dim strItem()
Dim j 
strItem = [COLOR=red yellow]Split(strText, ".")[/color red yellow]
For j = 0 To UBound(strItem)
 MsgBox(strItem(j))
Next


[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
sorry to bother you with this.

This shouldn't be this hard.

I tried that already and
I got the type mismatch on
strItem = Split(strText, ".")


 
Again, just a matter of me not paying attention. Loose the () when you Dim strItem.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Got it.
Thank you for your help.

It didn't like the array declaration

Dim strItem()

I changed it to
Dim strItem

and works fine.

Thanks again.
 
Thank you.
I see you got me the same answer.

Gee...spent too much time on this.

You are best.

 
Thanks for the star, but if I were best, I would have caught all the errors first time through.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Based on my comments in thread329-762207:
Dim code(1) ' static array, you cannot use ReDim later
Dim code() ' dynamic array, you can use ReDim later
Dim code ' variable

"It didn't like the array declaration"

I learned something here since I did not realize split() required a DIM as a variable not array. I even went over to devguru.com and reviewed their examples for DIM and SPLIT(). The description in the SPLIT() reference examples did not address this specific issue at all since it did not show the variable being dimensioned.

Why would SPLIT() want to put its results in array form into a memory variable rather than an array? I decided to test it a bit further...

Dim strItem created a variable with varType() of 0 which is the constant vbEmpty, which is to be expected.

After the SPLIT() the variable was changed to varType() of 8204 which is not on the list of VB constants. However, it must represent the combination of 2 constants:

VB constant vbArray is 8192 - Array
VB constant vbVariant is 12 - Variant (only use for arrays of variants)

So there we have it, if the destination variable has already been dimensioned, then SPLIT() requires it to be a variable, not an array type, and transforms it into an array.

dbMark
 
Great! Here's the question I've been searching for, but no answer. :)

If you would need to Split a String into an Array after it have been declared as an Array (in my case by an earlier Split), how would you go about?

You cannot ReDim back to a simple 'Variable' once you have an Array. I cannot use a second Variable and then copy it either (and it's no good answer anyways :)), since I will iterate this an arbitrary number of times (up to the user).

Any ideas?

//Magnus
 
I'm not sure I understand the question, but if you set the array = Nothing then you can split again to recreate a new array. As an example:
Code:
Option Explicit

Dim arr, strText
strText = "one.two.three"
arr = Split(strText, ".")
WScript.Echo UBound(arr)

Set arr = Nothing

strText = "one.two"
arr = Split(strText, ".")
WScript.Echo UBound(arr)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Aah, thanks mate, turns out I had only tested with "arr = Nothing", that is with the "Set".

And that was exactly what I was asking about, just in a dumb way. Thanks again. :)

//Magnus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top