For example, if I had:
Sub TestModule()
Dim test As String
test = "Machine Head"
End Sub
How would I extract the word Machine and assign it to a variable? I have tried using the Search and Left functions but have not had much success.
Cheers!
METHOD 1. Return First name from name. Output Range: Select the output range by changing the cell reference ("D5") in the VBA code. Name: Select the name from which you want to only extract the First name by changing the cell reference ("B5"), in the VBA code, or changing the name in cell ("B5").
Step 1: In the same module declare a sub-function to start writing the code for sub-function. Step 2: Declare two Variables A as string and B as String array and take input string from the user and store it in Variable A. Step 3: Use the Split SubString function and store its value in Variable B.
The syntax of VBA InStr is “InStr([start],string1,string2,[compare]).” In comparison, the syntax of InStrRev is “InStrRev(string1,string2,[start,[compare]]).” If the “start” argument is omitted, the InStr begins to search from the starting (first position) of the string.
Use Split():
Sub TestModule()
Dim test As String
dim frstWrd as string
test = "Machine Head"
frstWrd = split(test," ")(0)
Debug.Print frstWrd
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With