以下 1 班轮将执行您在第一句话中解释的内容:
Range(Worksheets("Sheet1").Cells(1, 3), Worksheets("Sheet1").Cells(1000, 3)).Replace "Category1", "DifferentCategory"
或者更容易阅读的版本:
Dim targetRange As Range
With Worksheets("Sheet1")
Set targetRange = .Range(.Cells(1, 3), .Cells(1000, 3))
targetRange.Replace "Category1", "DifferentCategory"
End With
如您所见,数组不是必需的,因为您正在寻找一个值并替换它。
但是,如果您真的想使用数组,这是更快的方法之一:
Dim myArray
Dim i As Integer
With Worksheets("Sheet1")
'myArray will now be a (1 to 1000, 1 to 1) array with all the cell values.
myArray = .Range(.Cells(1, 3), .Cells(1000, 3))
End With
For i = 1 To 1000
If myArray(i, 1) = "Category1" Then myArray(i, 1) = "DifferentCategory"
Next