作者:十九世纪末的伦敦_371 | 来源:互联网 | 2023-05-24 13:30
什么是VBA字符串插值语法?它存在吗?
我想使用Excel VBA格式化字符串.我有一个变量foo,我想把它放在一个范围的字符串中.
Dim row as Long
row = 1
myString = "$row:$row"
我想将字符串中的$行插值为"1"
1> natancodes..:
您还可以构建自定义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!
到立即窗口.
2> Jerome Monti..:
我相信这很好用.
Dim row as Long
Dim s as String
row = 1
s = "$" & row & ":$" & row
除非你想要类似于Python或C#的{}
符号,否则这是执行它的标准方法.