Solved

How to obtain the fist word from a given string

Posted on 2004-09-05
6
171 Views
Last Modified: 2010-05-03
How can I programmitcally get the first word from a string if the format is the following?
Genesis: Chapter: 001
Exodus: Chapter: 001

I only need the first word, for example: Genesis or Exodus etc.


Node.Text is as follows: Genesis: Chapter: 001

Here is the code I'm using, it doesn't work and I've been working with this problem for hours.
    a% = Len(Trim(Node.Text))
    b% = 1
    Do Until a% = 0
        If Left$(Node.Text, b%) = ":" Then
            GoTo ExitDo
        Else
            Book_ID = Left$(Node.Text, b%)
        End If
        a% = a% - 1
        b% = b% + 1
    Loop
   
ExitDo:

Hint: I only need the string prior to the first colon.
0
Comment
Question by:mcdonald_g
[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
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11986418
Private Sub Command1_Click()
    Dim MyArr As Variant
    Dim MyString As String
    MyString = Node.Text
    MyString = Replace(MyString, ":", " ")
    MyArr = Split(MyString, " ")
    MsgBox MyArr(0)
End Sub
0
 
LVL 8

Expert Comment

by:ampapa
ID: 11986421
Use the split function


dim strData

strData = split ("Genesis: Chapter: 001",":")

msgbox "First word is: " strdata(0)



   
 
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 125 total points
ID: 11986429
If you know it's always a text in front of the first colon it's even more straigh forward:

Private Sub Command1_Click()
    Dim MyArr As Variant
    MyArr = Split(Node.Text, ":")
    MsgBox MyArr(0)
End Sub
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 8

Expert Comment

by:ampapa
ID: 11986460
Even easier than that....    

a% = Len(Trim(Node.Text))
strData = split (Node.Text,":")
a% = strData(0)
0
 
LVL 8

Expert Comment

by:ampapa
ID: 11986465
Sorry, goofed on the variable

a% = Len(Trim(Node.Text))
strData = split (a%,":")
a% = strData(0)
0
 
LVL 8

Expert Comment

by:ampapa
ID: 11986573
hmmm...
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

695 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