【发布时间】:2011-05-18 02:51:26
【问题描述】:
我最近在很长一段时间没有需要后进入 Excel 宏开发。
我有一列有两百行,每行都有一个值。我写了一个循环来迭代每一行值,读取当前值,然后将值写回减去最后一个字符。
这是我编写的一些实际(和伪)代码。
Dim theRow as Long
Dim totRow as Long
Dim fooStr as String
theRow = 2 'we begin on the second row of the colummn
totRow = 201 'there are 200 values
For theRow = 2 to totRow
fooStr = WorkSheets(DestSheet).Cells(theRow,"A").Formula 'read the cell value
fooStr = Left(fooStr,Len(fooStr)-1 'subtract the last character from the value
Cells(theRow,1).Value = fooStr 'write the value back
Next theRow
在阅读了一些内容后,我了解到使用 Range 读取和写入值是最佳实践。是否可以使用 Range 重写我正在做的事情,这样它会更快。
这是我到目前为止的想法。
Range("A2:A201").Value = Len(Range.Left("A2:A201").Value)-1
但是,这不起作用。
如果确实可行,有什么线索可以说明如何做到这一点吗?
感谢任何提示。
【问题讨论】:
-
你说的是循环遍历范围内的每个元素吗?我相信仍然必须有一个循环,我认为您不能一次执行所有操作。