A simple question

C

Charlie

How would I modify this code to check first to see that there is indeed
Phrases(0), Phrases(1), and Phrases(2) so I won't get an error if strSentence
does not have 2 tabs in the sentence?
thanks,
ck

Phrases = Split(strSentence, vbTab)
str1 = Phrases(0) ' so you can see what they are
str2 = Phrases(1)
str3 = Phrases(2)
 
G

Greg Maxey

I suppose you could look for and count them first:

Sub Test()
Dim Phrases As Variant
Dim strSentence As String
Dim str1 As String, str2 As String, str3 As String
Dim i As Long
strSentence = Selection.Text
With Selection.Find
.Text = vbTab
While .Execute
If .Found Then i = i + 1
Wend
End With
If i = 2 Then
Phrases = Split(strSentence, vbTab)
str1 = Phrases(0) ' so you can see what they are
str2 = Phrases(1)
str3 = Phrases(2)
End If
End Sub
 
J

Jezebel

Phrases = Split(strSentence, vbTab)
For i = 0 to ubound(Phrases)
Select case i
Case 0
str1 = Phrases(0)
Case 1
str1 = Phrases(1)
Case 2
str1 = Phrases(2)
case else
end select
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top