VBA - 确定单元格值 (String) 是否与命名范围内的值 (String) 匹配
VBA - determine if a cell value (String) matches a Value (String) in a named range
很抱歉,如果这个问题已经得到解答,尽管我搜索了又搜索都没有成功。简而言之,如果单元格值与命名范围内的值不匹配,我将尝试更改单元格颜色。
虽然 none 对我有用,但我已经尝试了很多方法,非常感谢 vba 专家的任何帮助。
本质上我在 sheet1(Create) G2:G5000 上有一个值列表,我需要知道它们何时与 sheet2(lists) S2:S64 上的值不匹配 <--这有一个Make 的命名范围。
请在下面查看我当前代码的副本
Sub testMake()
Dim MkData As Range, MkVal As Range
Dim MKArray As Variant
Set MkData = Worksheets("Create").Range("G2:G5000")
Set MkVal = Worksheets("Lists").Range("Make")
For Each MyCell In MkData
If MyCell.Value <> Range("MkVal") Then
MyCell.Interior.ColorIndex = 6
Else
MyCell.Interior.ColorIndex = xlNone
End If
Next
End Sub
提前感谢大家的帮助,我已经看了几天了,似乎没有比开始时更接近了。
虽然我会使用条件格式,但您可以按以下方式稍微调整您的代码以编程方式执行此操作:
Sub testMake()
Dim MkData As Range
Dim MkVal As Range
Dim MKArray As Variant
Dim lngRow As Long
Dim rng1 As Range
Dim rng2 As Range
MKArray = Worksheets("Create").Range("G2:G5000").Value2
Set rng1 = Worksheets("Create").Range("G2")
Set MkVal = Range("Make")
For lngRow = 1 To UBound(MKArray)
If IsError(Application.Match(MKArray(lngRow, 1), MkVal, 0)) Then
If Not rng2 Is Nothing Then
Set rng2 = Union(rng2, rng1.Offset(lngRow - 1, 0))
Else
Set rng2 = rng1.Offset(lngRow - 1, 0)
End If
End If
Next
If Not rng2 Is Nothing Then rng2.Interior.ColorIndex = 6
End Sub
您可以使用工作表函数 Vlookup 来比较两个范围:
Sub testMake()
Dim MkData As Range, MkVal As Range
Dim MKArray As Variant
Dim result As Variant
Set MkData = Worksheets("Create").Range("G2:G5000")
Set MkVal = Worksheets("Lists").Range("Make")
For Each MyCell In MkData
On Error Resume Next
result = Application.WorksheetFunction.VLookup(MyCell, MkVal, 1, False)
If Err <> 0 Then
result = CVErr(xlErrNA)
End If
If Not IsError(result) Then
MyCell.Interior.ColorIndex = xlNone
Else
MyCell.Interior.ColorIndex = 6
End If
Next
End Sub
很抱歉,如果这个问题已经得到解答,尽管我搜索了又搜索都没有成功。简而言之,如果单元格值与命名范围内的值不匹配,我将尝试更改单元格颜色。
虽然 none 对我有用,但我已经尝试了很多方法,非常感谢 vba 专家的任何帮助。
本质上我在 sheet1(Create) G2:G5000 上有一个值列表,我需要知道它们何时与 sheet2(lists) S2:S64 上的值不匹配 <--这有一个Make 的命名范围。
请在下面查看我当前代码的副本
Sub testMake()
Dim MkData As Range, MkVal As Range
Dim MKArray As Variant
Set MkData = Worksheets("Create").Range("G2:G5000")
Set MkVal = Worksheets("Lists").Range("Make")
For Each MyCell In MkData
If MyCell.Value <> Range("MkVal") Then
MyCell.Interior.ColorIndex = 6
Else
MyCell.Interior.ColorIndex = xlNone
End If
Next
End Sub
提前感谢大家的帮助,我已经看了几天了,似乎没有比开始时更接近了。
虽然我会使用条件格式,但您可以按以下方式稍微调整您的代码以编程方式执行此操作:
Sub testMake()
Dim MkData As Range
Dim MkVal As Range
Dim MKArray As Variant
Dim lngRow As Long
Dim rng1 As Range
Dim rng2 As Range
MKArray = Worksheets("Create").Range("G2:G5000").Value2
Set rng1 = Worksheets("Create").Range("G2")
Set MkVal = Range("Make")
For lngRow = 1 To UBound(MKArray)
If IsError(Application.Match(MKArray(lngRow, 1), MkVal, 0)) Then
If Not rng2 Is Nothing Then
Set rng2 = Union(rng2, rng1.Offset(lngRow - 1, 0))
Else
Set rng2 = rng1.Offset(lngRow - 1, 0)
End If
End If
Next
If Not rng2 Is Nothing Then rng2.Interior.ColorIndex = 6
End Sub
您可以使用工作表函数 Vlookup 来比较两个范围:
Sub testMake()
Dim MkData As Range, MkVal As Range
Dim MKArray As Variant
Dim result As Variant
Set MkData = Worksheets("Create").Range("G2:G5000")
Set MkVal = Worksheets("Lists").Range("Make")
For Each MyCell In MkData
On Error Resume Next
result = Application.WorksheetFunction.VLookup(MyCell, MkVal, 1, False)
If Err <> 0 Then
result = CVErr(xlErrNA)
End If
If Not IsError(result) Then
MyCell.Interior.ColorIndex = xlNone
Else
MyCell.Interior.ColorIndex = 6
End If
Next
End Sub