什么是 VBA 字符串插值语法?它存在吗?
我想使用 Excel VBA 来格式化字符串。
我有一个变量 foo ,我想将它放入一个范围内的字符串中。
Dim row as Long
row = 1
myString = "$row:$row"
我希望字符串中的 $row 被插入为“1”
最佳答案
您还可以构建自定义 Format
功能。
Public Function Format(ParamArray arr() As Variant) As String
Dim i As Long
Dim temp As String
temp = CStr(arr(0))
For i = 1 To UBound(arr)
temp = Replace(temp, "{" & i - 1 & "}", CStr(arr(i)))
Next
Format = temp
End Function
用法与 C# 类似,只是不能直接引用字符串中的变量。例如。
Format("This will {not} work")
但是 Format("This {0} work", "will")
.Public Sub Test()
Dim s As String
s = "Hello"
Debug.Print Format("{0}, {1}!", s, "World")
End Sub
打印出来
Hello, World!
到立即窗口。
关于VBA 字符串插值语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28221349/