|
|
Visual Basic Editor in MS Excel can handle
strings just as good as a stand-alone VB program. All the string handling
functions in Visual Basic such as Left, Right, Instr, MId and Len can be used in
Visual Basic Editor. The following example illustrates the usage of all
these functions
Private Sub cmdInstr_Click()
Dim phrase As String
phrase = Cells(1, 1).Value
Cells(4, 1) = InStr(phrase, "ual")
End Sub
Private Sub cmdLeft_Click()
Dim phrase As String
phrase = Cells(1, 1).Value
Cells(2, 1) = Left(phrase, 4)
End Sub
Private Sub cmdLen_Click()
Dim phrase As String
phrase = Cells(1, 1).Value
Cells(6, 1) = Len(phrase)
End Sub
Private Sub cmdMid_Click()
Dim phrase As String
phrase = Cells(1, 1).Value
Cells(5, 1) = Mid(phrase, 8, 3)
End Sub
Private Sub cmdRight_Click()
Dim phrase As String
phrase = Cells(1, 1).Value
Cells(3, 1) = Right(phrase, 5)
|
Explanation:
1. InStr is a
function that looks for the position of a substring in a phrase.
InStr(phrase,"ual")
will find the substring "ual" from "Visual Basic" and then return
its position, in this case, it is 4 from the left.
2. Left is a
function that extracts characters from a phrase, starting from
the left. Left(phrase,4) means 4 characters are extracted from the
phrase, starting from the leftmost position.
3. Right is a
function that extracts characters from a phrase, starting from
the Right. Right(phrase,5) means 5 characters are extracted from the
phrase, starting from the rightmost position.
4. Mid is a function
that extracts a substring from a phrase, starting from the
posiiton specified by the second parameter in the
bracket.Mid(phrase,8,3) means a substring of three characters are
extracted from the phrase, starting from the 8th position from the
left
5. Len is a function
that return the length of a phrase.
|
|
 |
|
|