2012-05-13 43 views
1

删除空格我从细胞移动文本一些代码,细胞在VBA Excel中

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上的第一个代码。

回答

13

您应该使用
CStr

Str

则需要为Str

移除unncessary空间

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

从Excel帮助没有解决方法当数转换为字符串,一领先的空间总是保留给号码的符号。

+2

+1为了解决核心问题。也可以通过使用'f =“F”&CStr(startIndex + 1)来改变'toIndex = startIndex + 1'或者去掉'toIndex'' –