Note the use of the underscore in Listing This is VBAs code continuation characterits use ful for breaking up long statements into multiple lines for easier reading One caveatthough Make sure you add a space before the underscore or VBA will generate an e

The purpose of this procedure is to take a name (first and last, separated by a space, as shown in the TestIt procedure) and extract the last name. The full name is brought into the function as the fullName argument. After declaring an Integer variable named spacePos, the procedure uses the InStr function to check fullName and find out the position of the space that separates the first and last names. The result is stored in spacePos: spacePos = InStr(fullName, " ")

The real meat of the function is provided by the Mid$ string function, which uses the following syntax to extract a substring from a larger string:


string The string from which you want to extract the characters. In the

ExtractLastName function, this parameter is the fullName variable.

start The starting point of the string you want to extract. In

ExtractLastName, this parameter is the position of the space, plus 1 (in other words, spacePos + 1).

length The length of the string you want to extract. In the ExtractLastName function, this is the length of the full string—Len(fullName)—minus the position of the space.

Was this article helpful?

0 0

Post a comment