Hi,
I think at some point, most of the VBA programmers have to divide a name string, into multiple names.. eg. firstname and lastname.
I programmed this simple, but yet, needed function.
Since this is something I think people might need, I thought I could share the code with you.
ok, here goes:
I guess this code could be further optimized, if it's needed to be more dynamic. My variable for needle, could might as well be a constant. I guess it could be an optional pass-thru variable inside the function scope?
I however need it as it is, but I guess people can modify it to theire needs, as it's not very complex at all.
I found you can not use right() on the lastNam, as then it sometimes will start at wrong points in the variable.
I first tried working around this, but found it easier to stick with mid() and left()
Olav Alexander Mjelde
Admin & Webmaster
I think at some point, most of the VBA programmers have to divide a name string, into multiple names.. eg. firstname and lastname.
I programmed this simple, but yet, needed function.
Since this is something I think people might need, I thought I could share the code with you.
ok, here goes:
Code:
Function findName(sBigString As String)
' lets initialize the variables
Dim iPositionOfCharacter As Integer ' pos of needle
Dim sSubString As String 'search for?
sSubString = " " ' search for space (" ")
iPositionOfCharacter = InStrRev(sBigString, sSubString) ' search for the needle in the haystack
If iPositionOfCharacter <> 0 Then ' if it was found
firstNam = Left(sBigString, iPositionOfCharacter - 1) ' get the fnam
lastNam = Mid(sBigString, iPositionOfCharacter + 1) ' get the lnam
Else ' if not found
firstNam = sBigString ' set fnam to entire nam
lastNam = "-mangler-" ' set lnam to "-mangler-"
End If
End Function
I guess this code could be further optimized, if it's needed to be more dynamic. My variable for needle, could might as well be a constant. I guess it could be an optional pass-thru variable inside the function scope?
I however need it as it is, but I guess people can modify it to theire needs, as it's not very complex at all.
I found you can not use right() on the lastNam, as then it sometimes will start at wrong points in the variable.
I first tried working around this, but found it easier to stick with mid() and left()
Olav Alexander Mjelde
Admin & Webmaster