4

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

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”。

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

4

2 回答 2

15

你应该不
CStr
使用
Str

然后不需要解决方法来删除不必要的空间

IE

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

从 Excel 帮助Str

将数字转换为字符串时,始终为数字的符号保留前导空格。

于 2012-05-14T00:12:10.263 回答
8

您可以TRIM()在最终结果中使用toIndexorREPLACE空格,即

Replace ("alphabet", "a", "e")  'would return "elphebet"

从这里举起的例子:http ://www.techonthenet.com/excel/formulas/replace.php

所以...

f = Replace (f, " ", "")
于 2012-05-13T18:08:10.533 回答