使用VLOOKUPsearch不同的工作表

我试图search一个数字是否存在于我的工作簿中的32张中。 我试图使用下面提到的代码,但它不工作,因为VLOOKUP不解释variables(n)。 请帮助:

Private Sub SearchAll_Click() Dim SearchCriteria As Double, s As Integer SearchCriteria = Me.SearchBox.Value s = 0 For s = 0 To ThisWorkbook.Sheets.Count s = s + 1 If Application.WorksheetFunction.VLookup(SearchCriteria, Sheets(s).Range("A:A").Value, 1, False) = SearchCriteria Then MsgBox ("The Number " & SearchCriteria & " is available in list " & Sheets(s).Name) Exit For Else MsgBox ("The Number is Unavailable") End If Next s End Sub 

传说:

  • SearchAll是一个用来启动search的button。
  • SearchCriteria是一个文本框,用于input要search的值。

在使用Application.WorksheetFunction.VLookup来确定工作簿中是否存在特定值时,存在一些问题。 我已经将您的代码修改为以下内容:

 Private Sub SearchAll_Click() Dim SearchCriteria As Double, s As Integer Dim Lookup As Variant Dim Match As Boolean SearchCriteria = Me.SearchBox.Value For s = 1 To ThisWorkbook.Sheets.Count Lookup = Application.VLookup(SearchCriteria, Sheets(s).Range("A:A"), 1, False) If Not IsError(Lookup) Then MsgBox ("The Number " & SearchCriteria & " is available in list " & Sheets(s).Name) Match = True Exit For End If Next s If Match = False Then MsgBox ("The Number is Unavailable") End Sub 

在这里,我已经使用了Application.VLookup ,如果在特定工作表中找不到search值,它将向variablesvariablesLookup返回一个错误。 然后,通过查看查找的错误状态,可以确定是否findsearch值。 另外,为了避免每次在特定工作表中找不到该值时触发了The Number is Unavailable的消息。