vba Str 增加了额外的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17997729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:31:31  来源:igfitidea点击:

Str adds extra space

stringexcelvba

提问by user2642643

Dim i As Integer

i = Int((8 - 2 + 1) * Rnd + 2)

Dim rg As String
Dim con As String

con = Str(i)

rg = "B" & con
MsgBox (rg)

This returns "B 4" not "B4 anyone know the issue

这将返回“B 4”而不是“B4 任何人都知道这个问题

回答by Charles Williams

Use Cstr(i) rather than Str(i) - Cstr does not add a space

使用 Cstr(i) 而不是 Str(i) - Cstr 不添加空格

回答by Andy G

From the Help page for Str()

从帮助页面获取 Str()

When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied.

将数字转换为字符串时,始终为数字符号保留前导空格。如果 number 为正数,则返回的字符串包含前导空格并隐含加号。

回答by SeanC

Str()leaves space for the sign.

Str()为标志留下空间。

As Excel has implicit conversion, you can use rg = "B" & iand get the range you want

由于Excel具有隐式转换,您可以使用rg = "B" & i并获取您想要的范围

回答by matzone

Use format() function ...

使用 format() 函数...

con = format(i)

rg = "B" & con
MsgBox (rg)

回答by rangan

Use the Trim function to remove leading space as under:

使用 Trim 函数删除前导空格,如下所示:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim i As Integer

i = Int((8 - 2 + 1) * Rnd + 2)

Dim rg As String
Dim con As String

con = Str(i)

rg = "B" & Trim(con)
MsgBox (rg)
End Sub

回答by Elias

Excel Concatonate a string based on the int.

Excel 基于 int 连接一个字符串。

Here's a desciption

这是一个描述

https://stackoverflow.com/a/10004244/1504882

https://stackoverflow.com/a/10004244/1504882