我有以下代码循环遍历每一行并执行代码以更改值。我还希望它捕获每行C列中的处理器名称,但我不希望重复的名称。然后,我将这些名称用于我准备的自动电子邮件代码。我不知道如何将名称捕获到没有重复的每一行的变量中。
Dim x as Integer
Application.ScreenUpdating = False
NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
Range("A2").Select
For x = 2 To NumRows + 1
Range("A" & x).Validation.Delete
If ActiveSheet.Range("T" & x).Value > 1 Then ActiveSheet.Range("G" & x).Value = "YES" ' Else
If ActiveSheet.Range("T" & x).Value < 1 Then ActiveSheet.Range("G" & x).Value = "NO" ' Else
'HERE IS WHERE I WANT TO ADD THE CODE I NEED HELP WITH
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
答案 0 :(得分:0)
此应用程序非常容易使用集合。
在单元格中循环,您可以在集合中循环并检查重复项。然后使用布尔值设置是否存在重复项,从而设置是否将值添加到集合中。 这是一个示例结构,没有变量名。
[x] = var
Dim [collection] As New Collection, [cell] as Range, [range] as Range, [boolean] as Boolean, [item] as Variant
For Each [cell] In [range]
[boolean] = False
For Each [item] In [collection]
If [item] = [cell].Value Then
[boolean] = True
Exit For
End If
Next [item]
If [boolean] = False Then [collection].Add [cell].Value
Next [cell]
然后您可以再次使用for循环读取所有项目:
For Each [item] In [collection]
Debug.Print [item]
next [item]
答案 1 :(得分:0)
您可以尝试:
Option Explicit
Sub TEST()
Dim LastRow1 As Long, i As Long
Dim dict As New Scripting.Dictionary
With ThisWorkbook.Worksheets("Sheet1")
'Find Last row of sheet 1 column C
LastRow1 = .Cells(.Rows.Count, "C").End(xlUp).Row
'Start looping rows from row 2 to lastrow1
For i = 2 To LastRow1
'If item not exist in in dictionary
If Not dict.Exists(.Range("C" & i).Value) Then
'If dictionary is empty
If dict.Count = 0 Then
dict.Add .Range("C" & i).Value, dict.Count
Else
dict.Add .Range("C" & i).Value, dict.Count + 1
End If
End If
Next i
End With
End Sub