目的:是让我的代码查看第4列,找到“示例1”或“示例2”或“示例3”,然后将单个值返回到第29列的另一个单元格中。例如,D3是“示例3”,则AC3为“值”,D1839为“示例1”,然后AC1839为“值”
这是我的代码
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To FinalRow
If Cells(x, 4) = "Example 1" Or Cells(x, 4) = "Example 2" Or Cells(x, 4) = "Example 3" _
Or Cells(x, 4) = "Example 4" Or Cells(x, 4) = "Example 5" _
Or Cells(x, 4) = "Example 6" Then Cells(x, 29) = "AP"
Next x
奇怪的是,当我在一个小样本(即只有第4列和第29列的新表)上尝试时,它可以工作。
我正在寻求帮助,以寻找一种使代码有效的方法。包含180个示例的65,000行。
感谢您的帮助。我希望我的问题很清楚。
答案 0 :(得分:1)
遍历数组,并在AC列中构建单元的并集。
Dim arr As Variant, i As Long, rng As Range
arr = Range(Cells(1, "D"), Cells(Rows.Count, "D").End(xlUp)).Value
For i = LBound(arr, 1) To UBound(arr, 1)
Select Case arr(i, 1)
Case "Example 1", "Example 2", "Example 3", "Example 4", "Example 5", "Example 6"
If rng Is Nothing Then
Set rng = Cells(i, "AC")
Else
Set rng = Union(rng, Cells(i, "AC"))
End If
Case Else
'do nothing
End Select
Next i
If Not rng Is Nothing Then
rng = "AP"
End If