您试图检查Y列中的每个单元格是否来自另一个工作表的列表。 vlookup函数的结果总是“false”。我不知道为什么。请指教
Sub CheckDropDown()
Dim MyStringVar As Variant, i As Integer
Dim Lookup_Range As Range, lastRow As Integer, ws As Worksheet
Set Lookup_Range = Worksheets("Lists").Range("C1:C21")
lastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).row
For i = 2 To lastRow
On Error Resume Next
'MyStringVar = ActiveCell.FormulaR1C1 = "=VLOOKUP(Cells(i,25),Lists!C[-25],1)"
MyStringVar = ActiveCell.Formula = "VLookup(Cells(i, 25).value, Lookup_Range, 1, False)"
On Error GoTo 0
Select Case Cells(i, 25).value
Case IsEmpty(MyStringVar)
' do nothing
Case Is = MyStringVar
Case Is <> MyStringVar
ActiveSheet.Cells(i, 25).Interior.Color = RGB(255, 255, 5)
End Select
Next i
End Sub
答案 0 :(得分:0)
我的期望是你得到了这个意想不到的结果,因为你没有在ActiveCell中编写有效的公式。有效的公式是
=VLOOKUP(Y2, 'Lists'!C1:C21, 1, FALSE)
所以创建你的代码的方式应该是引号之间的关系&#34;&#34;。
如果你把一个有效的公式放在那里,那就和你自己在excel-sheet上写的那个一样,那么它就可以了。现在,您将界面中的excel-formula与编程语言混合在一起。
这样的事情:
Dim myFormula As String
myFormula = "=VLOOKUP("
Dim columnLetter As String
columnLetter = "Y"
Dim tmpFormula As String
For i = 2 to x
tmpFormula = myFormula & columnLetter & i & ", 'lists'!C1:C25, 1, FALSE)"
.. test formula .. etc.
Next i
成功
PS。如果你需要&#39; =&#39;不要再记得了。在公式前面,所以试试那个。
答案 1 :(得分:0)
当你只用其中一个解决问题时,我不喜欢组合公式和代码,所以我建议你使用纯代码来检查这些代码作为基础的值。
Sub CheckDropDown()
Dim MyStringVar As Variant, i As Integer
Dim Lookup_Range, cel As Range, lastRow As Integer, check As Boolean
Set Lookup_Range = Worksheets("Lists").Range("C1:C21")
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
For Each cel In Lookup_Range
If ActiveSheet.Cells(i, 25) = cel.Value Then
check = True: Exit For
Else
check = False
End If
Next
If check Then
'The cell is on the lookupRange
Else
'The cell is NOT on the lookupRange
ActiveSheet.Cells(i, 25).Interior.Color = RGB(255, 255, 5)
End If
Next i
End Sub