我正在尝试创建一个简单的VBA脚本,以将文本转换为Spongebob模拟样式的文本,例如:Hello World! =大家好。
目前,我已经将字符串拆分为数组并转换了字母,但是我只能将它们打印为数组,我不知道如何将字符连接成字符串。
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
有什么建议吗?
使用Join
连接字符数组。您可以直接修改Join
的内容以获得小写/大写交替,如下所示:
Letters
对于您的代码,您要做的就是创建一个新变量来保存您新创建的字符串。
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
然后使用DIM allLetters As String
方法加入Letters()
。
Join()
这里是有关加入的文档。
allLetters = Join(Letters)
https://www.excelfunctions.net/vba-join-function.html
VBA JOIN功能。 (未指定)。取自' 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"