我有一个超过500行的数字列。我需要使用VBA检查变量X是否与该列中的任何值匹配。
有人可以帮帮我吗?
使用范围的find方法比手动循环遍历所有单元格要快。
下面是在VBA中使用find方法的示例
Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("Sheet1").Range("A:A") 'searches all of column A
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True 'value found
Else
MsgBox "Nothing found" 'value not found
End If
End With
End If
End Sub
最简单的方法是使用Match
函数。
If Not IsError(Application.Match(ValueToSearchFor, RangeToSearchIn, 0)) Then
' String is in range
Application.WorksheetFunction.Match
可以提供自动完成的访问。 - Andy G如果您想在不使用 VBA 的情况下完成此操作,您可以结合使用 IF
、ISERROR
和 MATCH
。
因此,如果所有的值都在列 A 中,将此公式输入列 B:
=IF(ISERROR(MATCH(12345,A:A,0)),"Not Found","Value found on row " & MATCH(12345,A:A,0))
这将查找值为 "12345" 的内容(也可以是单元格引用)。如果未找到该值,MATCH
将返回 "#N/A",而 ISERROR
会捕获它。
如果您想使用VBA,则最快的方法是使用FOR循环:
Sub FindMatchingValue()
Dim i as Integer, intValueToFind as integer
intValueToFind = 12345
For i = 1 to 500 ' Revise the 500 to include all of your values
If Cells(i,1).Value = intValueToFind then
MsgBox("Found value on row " & i)
Exit Sub
End If
Next i
' This MsgBox will only show if the loop completes with no success
MsgBox("Value not found in the range!")
End Sub
你可以在VBA中使用工作表函数,但它们很挑剔,有时会抛出毫无意义的错误。使用FOR
循环比较稳妥。
=countif(A:A,12345)>0
如果找到该数字就会返回True,否则返回False。 - nutschSheets("Codes").Cells(i,1).Value
@scott 正确,但如果用户不检查许多值,可能更容易在范围上实现FOR
循环。 如果@user1547174想要使用此方法,请查看MSDN上的FIND
方法:http://msdn.microsoft.com/en-us/library/office/ff839746.aspx - Jake BathmanApplication.WorksheetFunction.CountIf
也非常有效。 - ashleedawg试一下这个:
If Application.WorksheetFunction.CountIf(RangeToSearchIn, ValueToSearchFor) = 0 Then
Debug.Print "none"
End If
Function FindFirstInRange(FindString As String, RngIn As Range, Optional UseCase As Boolean = True, Optional UseWhole As Boolean = True) As Variant
Dim LookAtWhat As Integer
If UseWhole Then LookAtWhat = xlWhole Else LookAtWhat = xlPart
With RngIn
Set FindFirstInRange = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=LookAtWhat, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=UseCase)
If FindFirstInRange Is Nothing Then FindFirstInRange = False
End With
End Function
MsgBox FindFirstInRange(StringToFind, Range("2:2"), TRUE, FALSE).Address
MsgBox FindFirstInRange(StringToFind, Range("2:2")).Address
MsgBox FindFirstInRange(StringToFind, Range("2:2")).Address
。它会抛出“对象必需”错误。 - JeffC修复了@JeffC在@sdanse函数中提到的问题:
Function FindFirstInRange(FindString As String, RngIn As Range, Optional UseCase As Boolean = True, Optional UseWhole As Boolean = True) As Variant
Dim LookAtWhat As Integer
If UseWhole Then LookAtWhat = xlWhole Else LookAtWhat = xlPart
With RngIn
Set FindFirstInRange = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=LookAtWhat, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=UseCase)
If FindFirstInRange Is Nothing Then
FindFirstInRange = False
Exit Function
End If
If IsEmpty(FindFirstInRange) Then
FindFirstInRange = False
Else
FindFirstInRange = True
End If
End With
End Function
If Not IsError(Application.WorksheetFunction.Match(ValueToSearchFor, RangeToSearchIn, 0)) Then
' String is in range