First off, while VBA doesn't have a ton of built-in functions to work with
Concatenating one or more
Let's say you have a form prompting a user (let's call him Joe Smith) to enter their first and last name into two different fields (let's call them
Dim sUserName As String, LName As String, FName As StringBut that's not quite right, as we end up with
LName = "Smith"
FName = "Joe"
sUserName = FName & LName' Stick 'em together with "&".
Dim sUserName As String, LName As String, FName As StringIf you wanted to save this as
LName = "Smith"
FName = "Joe"
sUserName = FName & " " & LName' Note the extra "&" surrounding the space.
Dim sUserName As String, LName As String, FName As String
LName = "Smith"
FName = "Joe"
sUserName = LName & ", " & FName' Nothing too fancy required.
Inserting a character into a
Perhaps you got ahead of yourself and didn't think to program in your comma and space from the example above, and the computer thinks it's working with some guy named
First, we can use the
Dim sUserName As StringWhat's happening here is the
sUserName = "SmithJoe"
sUserName = Left(sUserName, 5) & ", " & Right(sUserName, 3)
Compare this with essentially the same job in Python:
s = 'SmithJoe'
s = s[:5] + ', ' + s[5:]
print(s)# s = 'Smith, Joe'
Finding a
If we know this is our user's name, this works just fine, but this is usually not going to be the case. If John Doe was using our application, then the code above would return
First, let's change our code above to assign the parameters used for
iLeftCount = 5As is, this code will still produce the same result, but now we are able to change those values as needed for other names. To get to meaningful numbers, we need to look at the
iRightCount = 3
sUserName = Left(sUserName, iLeftCount) & ", " & Right(sUserName, iRightCount)
The next thing we can do is loop through our
Dim sUserName As String
Dim iStrLen As Integer, iLeftCount As Integer, iRightCount As Integer
Dim iLoopCount As Integer
Dim sCheckLetter As String
sUserName = "DoeJohn"
iStrLen = Len(sUserName)
iLeftCount = 0
iRightCount = 0
For iLoopCount = 2 To iStrLen
' Start at 2 to skip the first letter, since we already know this is the first letter of a name.
sCheckLetter = Mid(sUserName, iLoopCount, 1)
If sCheckLetter = UCase(sCheckLetter) Then
' We found a capital letter, which is one character past the name we want,
' so count back one space.
iLeftCount = iLoopCount - 1
' Using the same logic, count back from the end to get the other name.
iRightCount = iStrLen - (iLoopCount - 1)
' We've found the break, so we can skip looping through the rest.
Exit For
End If
Next iLoopCount
If iLeftCount <> 0 And iRightCount <> 0 Then
sUserName = Left(sUserName, iLeftCount) & ", " & Right(sUserName, iRightCount)
End If
Doing a bit more...
While using
Let's say you wanted to insert a character between each character of a
Example 1:
Dim sString As StringExample 2:
Dim sNewString As String
Dim iStrLen As Integer
Dim iLoopCount As Integer
sString = "Thisismystring"
sNewString = ""
iStrLen = Len(sString)
For iLoopCount = 1 To iStrLen
' Add the current character from the original String to the end of the new String.
sNewString = sNewString & Mid(sString, iLoopCount, 1)
' We will only add the new character IN the String, not at the end.
If iLoopCount < iStrLen Then
sNewString = sNewString & vbCr
End If
Next iLoopCount
Dim sString As StringExample 3 (using an array trick):
Dim iStrLen As Integer
Dim iLoopCount As Integer
sString = "Thisismystring"
iStrLen = Len(sString)
' We will only add the new character IN the String, not at the end, so start on back from the end.
For iLoopCount = iStrLen - 1 To 1 Step -1
' Insert the new character into the original String
sString = Left(sString, iLoopCount) & vbCr & Mid(sString, iLoopCount + 1)
Next iLoopCount
Dim sString As String
Dim iStrLen As Integer
Dim iLoopCount As Integer
sString = "Thisismystring"
iStrLen = Len(sString)
' We will only add the new character IN the String, not at the end, so start on back from the end.
For iLoopCount = iStrLen - 1 To 1 Step -1
' Insert the new character into the original String
sString = Left(sString, iLoopCount) & vbCr & Mid(sString, iLoopCount + 1)
Next iLoopCount
That's not by any means all you can do with