我需要让 Evaluate 为 Replace 函数工作。 该代码应该用任何内容替换“-0700”。 下面提供了代码以及 C 列中的数据:
Set sh = ThisWorkbook.Sheets(1)
Set rangc = sh.Range("c2:c10")
For Each area In rangc.Areas
area.Value = Evaluate("IF(ROW(" & area.Address & ")," & Replace(area.Address, " -0700", "") & ")")
Next area
-这是表中的数据:
ColumnC(original data)
-0700
sdfg -0700
how -0700 saf
2016-10-16 10:13:41 -0700
ColumnC(After code is run)
-700
sdfg -0700
how -0700 saf
10/16/2016 10:13
为什么代码对第一行(-700)做了一些事情,但没有替换为“”。 它还删除了第 4 行中的“-700”(2016 年 10 月 16 日 10:13)。 但它不适用于第二行和第三行? 如何使其适用于字符串或数字?
我正在尝试使用类似的代码非常快速地替换另一个字符串中的部分字符串。 我不能接受使用循环方法,因为有数十万行。
您不需要循环遍历整个范围。你可以使用
.Replace
方法,速度相当快:
Set sh = ThisWorkbook.Sheets(1)
Set rangc = sh.Range("c2:c10")
rangc.Replace What:=" -0700", Replacement:=""
代码中有一些错误
Set Sh = ThisWorkbook.Sheets(1)
Set rangc = Sh.Range("c2:c10")
For Each area In rangc.Cells 'iterate through the cells
area.value = Evaluate("IF(ROW(" & area.Address & "),""" & Replace(area, " -0700", "") & """)")
Next area
在 Replace 方法中引用区域对象而不是它的字符串地址。当它创建不带“-0700”的文本时,生成的文本也应该用括号括起来,以用于
IF
函数。