我正在运行一个宏,它根据用户输入的数字进行过滤。然后它接受该数字并根据该值过滤列表。我需要验证,如果用户输入的数字不在列表中,它将退出宏。我想我很接近,但我是VBA的新手,所以我一直卡住了。任何帮助都会很棒!
谢谢!
'Filter based on user entry
Sheets("New Revision ").Select
part = Range("B4").Value
Sheets("PN_List").Select
Columns("D:E").Select
Selection.EntireColumn.Hidden = False
ActiveSheet.Range("$A$1:$K$3000").AutoFilter Field:=1, Criteria1:=part
If ActiveSheet.Range("$A$1:$K$3000").AutoFilter Field=1, Criteria1="" Then
MsgBox "Part number not found please try again.", vbOKOnly + vbExclamation, "Entry Error"
Exit Sub
End If
答案 0 :(得分:2)
Sheets("New Revision ").Select
part = Range("B4").Value
Sheets("PN_List").Select
If Application.Countif([A1:A3000], part) < 1 Then
MsgBox "Part number not found please try again.", vbOKOnly + vbExclamation, "Entry Error"
Exit Sub
End If
Sheets("PN_List").Columns("D:E").EntireColumn.Hidden = False
ActiveSheet.Range("$A$1:$K$3000").AutoFilter Field:=1, Criteria1:=part
答案 1 :(得分:0)
试试这个:
Sub Parts()
Dim Part As Variant
Dim Found As Boolean
Part = Sheets("New Revision").Range("B4").Value
On Error Resume Next ' If the lookup fails, ignore the error
' the following assumes Part is in Column A
Found = WorksheetFunction.Match(Part, Sheets("PN_List").Range("$A$1:$A$3000"), 0) > 0
On Error GoTo 0 ' Resume normal error handling
If Not Found Then
MsgBox "Part number not found please try again.", vbOKOnly + vbExclamation, "Entry Error"
End If
End Sub
编辑:您仍然可以根据需要进行过滤。也许这样做的好地方是Else
If Not Found
条款。