【问题标题】:Two TextBox one Answer VBA6两个文本框一个答案 VBA6
【发布时间】:2015-06-27 05:10:21
【问题描述】:

在我的用户窗体中,我有两个文本框。我希望能够通过任一文本框查找,但只使用一个或另一个。如果两者都是空的,我想要一个 MSGBOX 告诉用户输入信息。我可以让我的代码做一个/或文本框输入,但不能让用户跳过 TextBox1 或不输入任何内容的两个文本框。 这是我的代码.....

Private Sub OkayCommandButton_Click()
Worksheets("Parts List").Select
Application.ScreenUpdating = False
Range("A2").Select
PN = PartNumber.Value
KN = KanbanNumber.Value
If PartNumber = vbNullString Then
   MsgBox "Please enter a Part Number"
   PartNumber.SetFocus
Else
   Cells.find(What:=PN, After:=Range("A2"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
End If
If Kanban = vbNullString Then
   MsgBox "Please enter a Kanban Number"
   PartNumber.SetFocus
Else
   Cells.find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False, SearchFormat:=False).Activate
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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以先检查两个 TextBox 元素是否为空,然后向用户发送消息。如果该检查失败,则一个或两个 Text-Box-Elements 包含文本。那里有冲突,因为如果两个文本框都包含搜索字符串,您只想使用一个 TextBox-Element 进行搜索。在这种情况下,您必须为其中一个文本框提供优先级(您检查的第一个获胜):

    这只是你文本的一个sn-p:

    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
    

    【讨论】:

    • 我按照你的方式保存了它。由于您分配了两个变量 PN 和 KN,因此最好在 IF-ELSE 语句中检查它们,而不是直接检查 PartNumber 和 KanbanNumber ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多