Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to split variable containing words with spaces into multiple variables in VB6

Posted on 2004-04-13
19
Medium Priority
?
347 Views
Last Modified: 2010-05-18
Hey guys,

I need a way to seperate this line into multiple variables:

VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "

In other words I need each item in that var into seperate vars ie a is = to AT1/0.527 b = 10.13.3.110 etc...

This is being used inside a for next loop and I would like this done in one pass so if possible no for next loops (unless you think it will work!)

Please provide working code for answer to be accepted!  Thanks!!
0
Comment
Question by:bluedragon99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 4
  • +1
19 Comments
 
LVL 3

Accepted Solution

by:
elantra earned 2000 total points
ID: 10818792
Try this:
-------------------------------
    Dim a() As String
    Dim VarSplitme As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    VarSplitme = Replace(VarSplitme, "     ", vbTab)
    a() = Split(Trim(VarSplitme), vbTab)
    MsgBox a(0)
-------------------------------
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10818806
That gets the first word in the string I need the others in variables also.  Getting there :)
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10818863
maybe a do loop with a split function searching for spaces??
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Dabas
ID: 10818915
Hi bluedragon99,
> That gets the first word in the string I need the others in variables also.
Not really
a(0) is the first string (AT1/0.527)
a(1) is the seconds (10.13.3.110)
a(2) the third.

If you want to loop through them, then you can try
For I = 0 to UBound(a)
    MsgBox a(i)
Next

Dabas
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10818924
bluedragon99,
Just had another read at your question.
What is separating the different sections of your string:
1) A fixed number of spaces?
2) A tab
3) A Variable number of spaces?

Dabas
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10818943
variable # of spaces
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10818973
This puts them on different lines in a msg box, gonna use split and search for vbcrlf I think that will work

 Dim myString
    myString = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    myString = Replace(myString, " ", vbCrLf)
    MsgBox myString
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10818996
bluedragon99,
Actually does not matter. Elantra's code should still be OK, as he is using Trim
Maybe his code needs to be modified thus:

    Dim a() As String
    Dim VarSplitme As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    VarSplitme = Replace(VarSplitme, " ", vbTab) 'Only one space gets converted into a tab, to set a delimiter
    a = Split(Trim(VarSplitme), vbTab) 'The other spaces should get removed by trim here. NOtice also removal of brackets in a()
    For I = 0 to UBound(a)
        MsgBox a(i)
    Next    

Dabas
0
 
LVL 3

Expert Comment

by:elantra
ID: 10819034
This is it right here:

    Dim a() As String
    Dim b() As String
    Dim VarSplitme As String
    Dim j As Integer
    j = 0
    ReDim b(j) As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    a() = Split(Trim(VarSplitme))
    For i% = 0 To UBound(a)
        If a(i%) <> "" Then
            b(j) = a(i%)
            j = j + 1
            ReDim Preserve b(j) As String
            MsgBox b(j - 1)
        End If
    Next i%
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819127
remember I need to be able to retrieve each value how would I go about calling from the array?
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819173
like I said I really need this done without for's..
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819218
The reason I'm not using for/next is because for some reason for instance with elantras solution it does not return to next k% it goes to the next line in the main for loop.  any ideas why?
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819262
This solution from elantra is what I am looking for.  This works great without for/next junk except for the fact that the spacing is random.  Can anyone make this work with random spacing?
Dim a() As String
    Dim VarSplitme As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    VarSplitme = Replace(VarSplitme, "   ", vbTab)
    a() = Split(Trim(VarSplitme), vbTab)
   
    MsgBox a(1)
    MsgBox a(2)
    MsgBox a(3)
    MsgBox a(4)
    MsgBox a(5)
    MsgBox a(6)
    MsgBox a(7)
    MsgBox a(8)
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819442
cricket...cricket...cricket
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10819512
bluedragon99,
Have you tried my solution?

Dabas
0
 

Expert Comment

by:Meherab
ID: 10820875
'Try This (Without For/Next Loops)

Dim VarSplitme As String
Dim sTemp As String
Dim arrSplit() As String

Dim iLen As Integer
Dim iCount As Integer

    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
   
    'This removes extra Spaces
    sTemp = VarSplitme
    Do
      iLen = Len(sTemp)
      sTemp = Replace(sTemp, Space(2), Space(1))
    Loop Until iLen = Len(sTemp)
   
    'This Does the split into the array
    arrSplit = Split(Trim(sTemp), Space(1))
   
    'Loop the Array
    iCount = 0
    Do
      MsgBox arrSplit(iCount)
      iCount = iCount + 1
    Loop Until iCount > UBound(a)

    'U can use a For Loop here if you want
    For iCount = 0 to UBound(a)
      MsgBox arrSplit(iCount)
    Next

0
 
LVL 3

Expert Comment

by:elantra
ID: 10821086
My post in relation to "This is it right here:" @ 04/13/2004 05:31PM MST will work with random spacing.  Have you tried it?  I already tested it and it works for me.
0
 
LVL 3

Expert Comment

by:elantra
ID: 10821100
Here is the code you need.  It is exactly the same as above.  It will work with random spacing.  Each value will be separated into the b() array.  You can call each by using b(0) b(1) b(2) etc...  Enjoy.

   'Start here

    Dim a() As String
    Dim b() As String
    Dim VarSplitme As String
    Dim j As Integer
    j = 0
    ReDim b(j) As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    a() = Split(Trim(VarSplitme))
    For i% = 0 To UBound(a)
        If a(i%) <> "" Then
            b(j) = a(i%)
            j = j + 1
            ReDim Preserve b(j) As String
        End If
    Next i%

    MsgBox b(0)
    MsgBox b(1)
    MsgBox b(2)
    MsgBox b(3)

    'ETC... ETC...
0
 

Expert Comment

by:Meherab
ID: 10821316
I agree with you elantra.

Just put a different perspective on it

Cheers ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Progress
Starting up a Project

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question