Using Builtin Functions And Statements

With the Mid, Right, and Left functions, you do not always know the length of the strings and may need to check the string length to determine the number of characters to return before calling one of these functions. You can determine the length of a string with the Len function: Len(string), which uses only one argument, string, to identify the string to check. You can make the string argument an actual string, or the name of a varaible that contains a string. You can use a conditional statement along with the Len function to determine the number of characters to return from a string. The If Then statement in the following example checks to see if the length of the string is longer than ten characters. If so, the Right function returns a portion of the string.

Example:

Dim NewStr As String

Dim NewStr2 As String

Dim CharNum As Integer

Dim NewLength As Integer

NewStr = "This is a Sample String"

CharNum = Len(NewStr)

If CharNum > 10 Then

NewStr2 = Right(NewStr, CharNum - 10) End If

USING THE MID FUNCTION

□ Switch to Excel and run the associated macro.

■ The Mid function returns the specified number of characters from the string.

USING THE MID FUNCTION

_D Create a new subroutine.

-B Type Dim IB As String, replacing IB with the string variable.

Declare additional variables for the subroutine.

replacing IB with the variable in step 2, and "Text" with the input box text.

NC) replacing Cells(1, 1) with the result's location, MS with the start number and NC with the number of characters.

□ Switch to Excel and run the associated macro.

■ The Mid function returns the specified number of characters from the string.

0 0

Post a comment