用来自Codes!$A$1:$A$14 的适当值填充values 数组。
没有 cmets 的代码
Sub UpdateLookups()
Dim data, values As Variant
Dim Target As Range
Dim x As Long
values = Array("Tom", "Henry", "Frank", "Richard", "Rodger", "ect...")
With Worksheets("Sheet1")
Set Target = .Range("D2", .Range("D" & .Rows.Count).End(xlUp))
End With
data = Target.Value
For x = 1 To UBound(data, 1)
data(x, 1) = IIf(IsError(Application.Match(data(x, 1), values, 0)), "YES", "NO")
Next
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Target.Offset(0, -3).Value = data
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
End Sub
带有 cmets 的代码
Sub UpdateLookups()
Dim data, values As Variant
Dim Target As Range
Dim x As Long
'values: Array of values that will be searched
values = Array("Tom", "Henry", "Frank", "Richard", "Rodger", "ect...")
'With Worksheets allows use to easily 'qualify' ranges
'The term fully qualified means that there is no ambiguity about the reference
'For instance this referenece Range("A1") changes depending on the ActiveSheet
'Worksheet("Sheet1").Range("A1") is considered a qualified reference.
'Of course Workbooks("Book1.xlsm").Worksheet("Sheet1").Range("A1") is fully qualified but it is usually overkill
With Worksheets("Sheet1")
'Sets a refernce to a Range that starts at "D2" extends to the last used cell in Column D
Set Target = .Range("D2", .Range("D" & .Rows.Count).End(xlUp))
End With
' Assigns the values of the Target Cells to an array
data = Target.Value
'Iterate over each value of the array changing it's value based on our formula
For x = 1 To UBound(data, 1)
data(x, 1) = IIf(IsError(Application.Match(data(x, 1), values, 0)), "YES", "NO")
Next
Application.ScreenUpdating = False 'Speeds up write operations (value assignments) and formatting
Application.Calculation = xlCalculationManual 'Speeds up write operations (value assignments)
'Here we assign the data array back to the Worksheet
'But we assign them 3 Columns to the left of the original Target Range
Target.Offset(0, -3).Value = data
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
'Loading the data into an Array allows us to write the data back to the worksheet in one operation
'So if there was 100K cells in the Target range we would have
'reduced the number of write operations from 100K to 1
End Sub