Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果查找未找到输入VBA6_Excel_Vba - Fatal编程技术网

Excel 如果查找未找到输入VBA6

Excel 如果查找未找到输入VBA6,excel,vba,Excel,Vba,我刚刚意识到,如果用户输入的不是数据库中的内容(excel电子表格),该怎么办?我已经把所有这些贴子都贴满了,似乎没有一个能满足我的需要。请有人看一下我的密码好吗 Private Sub OkayCommandButton_Click() Worksheets("Parts List").Select Application.ScreenUpdating = False Range("A2").Select PN = PartNumber.Value KN = KanbanNumber.Valu

我刚刚意识到,如果用户输入的不是数据库中的内容(excel电子表格),该怎么办?我已经把所有这些贴子都贴满了,似乎没有一个能满足我的需要。请有人看一下我的密码好吗

Private Sub OkayCommandButton_Click()
Worksheets("Parts List").Select
Application.ScreenUpdating = False
Range("A2").Select
PN = PartNumber.Value
KN = KanbanNumber.Value
If ((PartNumber = vbNullString) And (KanbanNumber = vbNullString)) Then
   ' Both textboxes are empty, message box opened and focus to part number
   MsgBox "Please enter a Part Number or Kanban Number"
   PartNumber.SetFocus
Else
   ' One or more textboxes contain a search string
    If Not (PartNumber = vbNullString) Then
       'Part number is given, run search
       Cells.find(What:=PN, After:=Range("A2"), LookIn:=xlFormulas, _
       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
Else
   ' Part Number is not given
   ' Since we checked that at least one textbox contains text
   ' the Kanban Number must be set if Part Number has not been set
       Cells.find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False, SearchFormat:=False).Activate
    End If
End If
PartInformation.Caption = _
"Part Number" & vbTab & ActiveCell & vbCrLf & _
"Kanban" & vbTab & vbTab & ActiveCell.Offset(0, 45) & vbCrLf & _
"Part Name" & vbTab & ActiveCell.Offset(0, 1) & vbCrLf & _
"Supplier" & vbTab & vbTab & ActiveCell.Offset(0, 2) & vbCrLf & _
"Next Process" & vbTab & ActiveCell.Offset(0, 3) & vbCrLf & _
"Qty in Tote" & vbTab & ActiveCell.Offset(0, 44) & vbCrLf & _
"PC Location" & vbTab & ActiveCell.Offset(0, 46)
PartInformation1.Caption = "Line    " & ActiveCell.Offset(0, -1)
End Sub
的规范是,如果搜索项在搜索区域中不存在,它将返回
Nothing
Nothing
是一个特殊值,您可以对其进行测试

要处理不存在的搜索词,请尝试将
Dim rFindResult As Range
立即添加到
Private Sub.
行之后,然后更改单元格。查找如下语句:

Set rFindResult = Cells.Find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False, SearchFormat:=False)

If (rFindResult Is Nothing) Then
    ` display MesgBox, reset focus appropriately, exit sub
Else
    rFindResult.Activate
End If

Siddarth Rout关于指定要使用哪个工作表而不是隐式依赖活动工作表的观点值得注意。此外,如果既没有指定零件号也没有指定看板号,则在将焦点设置为零件号后,可能会有一个退出子项???它是否应该是
What:=PartNumber.text
,另一个也应该是类似的?您的
单元格()
范围()
也将引用
活动表。建议完全限定他们的资格。
ActiveSheet
可能不是您认为的工作表…Siddharth,很抱歉造成混淆。我已经把全部代码都放进去了。你现在能看一下吗?手推车。。。谢谢你的密码。我试过了,但不起作用。我将If(rFindResult为Nothing)Then语句移动到Set rFindResults下面和PartInformation.Caption上面。我没有异议。我也不能单步通过代码。它还提供来自UserForm上活动单元格的信息。我做错了什么?我让它工作了!!!!谢谢你的帮助!真的,你不知道这对我有多大帮助。感谢你花时间解释。