Reputation: 41
I'm trying to transform a text into a Spongebob mocking style text, like this:
Hello World! = hElLo WoRlD.
I split the string into an array and converted the letters, but I cant only print them as an array.
How do I join the characters into a string?
Private Sub CommandButton1_Click()
Dim Word As String
Dim i As Integer
Dim Letters() As String
Word = Sheet1.Range("A1").Value
Letters = Split(StrConv(Word, 64), Chr(0))
For i = 0 To Len(Word) - 1 Step 2
Debug.Print StrConv(Letters(i), 1)
Debug.Print StrConv(Letters(i + 1), 2)
Next i
End Sub
Upvotes: 0
Views: 821
Reputation: 50008
Use Join
to concatenate the array of characters. You can modify the contents of Letters
directly to get the lower-case/upper-case alternation, something like this:
Private Sub CommandButton1_Click()
Dim Word As String
Word = Sheet1.Range("A1").Value
Dim Letters() As String
Letters = Split(StrConv(Word, 64), Chr(0))
Dim i As Long
For i = LBound(Letters) To UBound(Letters)
If i Mod 2 = 0 Then
Letters(i) = StrConv(Letters(i), 2) '<~ or `LCase$`
Else
Letters(i) = StrConv(Letters(i), 1) '<~ or `UCase$`
End If
Next
Debug.Print Join(Letters, "")
End Sub
Upvotes: 1
Reputation: 2472
For your code all you need to do is create a new variable to hold your newly created string.
DIM allLetters As String
Then join the Letters()
using the Join()
method.
allLetters = Join(Letters)
Here's the documentation on joining.
https://www.excelfunctions.net/vba-join-function.html
' Join together the strings "John", "Paul" and "Smith".
Dim fullName As String
Dim names( 0 to 2 ) As String
names(0) = "John"
names(1) = "Paul"
names(2) = "Smith"
fullName = Join( names )
' The variable fullName is now set to "John Paul Smith"
VBA JOIN Function. (n.d.). Retrieved from https://www.excelfunctions.net/vba-join-function.html
Upvotes: 1