我似乎花了很多时间在电子表格中处理以下公式:
="some text '" & A1 & "', more text: '" & A2 &" etc."
Run Code Online (Sandbox Code Playgroud)
使用printf或String.Format字符串会更快
=String.Format ("Some text '{0}', more text: '{1}'",A1,A2)
Run Code Online (Sandbox Code Playgroud)
是否有类似于Excel内置的内容,或者我可以在不编写宏的情况下调用CLR吗?
Ale*_* K. 78
不,但您可以通过将以下内容添加到VBA模块来简单地创建一个天真的
public function printf(mask As String, ParamArray tokens()) as String
dim i as Long
for i = 0 To ubound(tokens)
mask = replace$(mask, "{" & i & "}", tokens(i))
next
printf = mask
end Function
Run Code Online (Sandbox Code Playgroud)
...
=printf("Some text '{0}', more text: '{1}'", A1, A2)
Run Code Online (Sandbox Code Playgroud)
小智 13
我已经更新了Alex的代码,因此您可以使用%s每次插入.
代替:
=printf("Some text '{0}', more text: '{1}'", A1, A2)
Run Code Online (Sandbox Code Playgroud)
您可以使用:
=printf("Some text '%s', more text: '%s'", A1, A2)
Run Code Online (Sandbox Code Playgroud)
就像原版一样sprintf.
更新的代码:
Public Function Printf(mask As String, ParamArray tokens()) As String
Dim i As Long
For i = 0 To UBound(tokens)
mask = Replace$(mask, "%s", tokens(i), , 1)
Next
Printf = mask
End Function
Run Code Online (Sandbox Code Playgroud)
Par*_*hod 10
你可以使用TEXT功能 -
你可以将格式字符串存储在像我一样的单元格中
我"BUY "#" CREDITS"在我的D1单元格中有价值在我的A5单元格中,我的值为5000.当我想显示我使用的格式化字符串时,=TEXT(A5,$D$1)
它会将单元格的值设置为BUY 5000 CREDITS
不是真的 - 有CONCATENATE功能
=CONCATENATE("some text '",A1,"', more text: '",A2," etc.")
Run Code Online (Sandbox Code Playgroud)
但它并不比&我认为的更好