gpt4 book ai didi

excel - 删除 VBA excel 中的空格

转载 作者:行者123 更新时间:2023-12-01 05:21:45 28 4
gpt4 key购买 nike

我有一些将文本从一个单元格移动到另一个单元格的代码

Dim startIndex As Integer
Dim toIndex As Integer
Dim f As String
Dim g As String

For startIndex = 50 To 60 Step 2
toIndex = Str(startIndex + 1)
f = "F" & Str(toIndex)
g = "G" & Str(startIndex)
Range(f).Value = Range(g).Value
Range(g).Value = ""
Next startIndex

但是变量 f 的值是“F 51”而不是“F51”。

如何解决这个问题?
p.s.这是我在 vba 上的第一个代码。

最佳答案

你应该使用CStr不是Str
然后不需要解决方法来删除不必要的空间

IE

 f = "F" & CStr(toIndex)
g = "G" & CStr(startIndex)

来自 Str 的 Excel 帮助

When numbers are converted to strings, a leading space is always reserved for the sign of number.

关于excel - 删除 VBA excel 中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10574000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com