在下拉菜单的循环中使用 Like 运算符
Use Like operator on a loop on dropdown menu
https://i.stack.imgur.com/3D3xS.png 我有 13 个下拉菜单。 for 循环有效(检查以确保我不会 select 同一设备和号码不止一次)。尝试在最后解决 If 语句,验证当我 select 设备 A、B 或“通道不可用”时,两个文本输入是否有值。 1) 如果设备 A 是 selected,请验证第一个文本输入框中是否提供了数字。
2) 如果设备 B 是 selected,请验证是否为第二个文本输入框提供了数字。 3) 验证至少为设备 a 或 b 文本输入框提供了一个数字。目前,我的消息正在弹出,但是当我单击“下一步”按钮时,没有任何反应?即使我在文本输入中输入值并单击下一步,似乎也没有任何反应。我认为这是我的 If 语句的结构,需要一些帮助来创建验证。有什么想法吗?
If (HTSelection.DeviceDropDown1.List(0)) <> Empty Then
Else
DeviceDropDown1.AddItem "Device A: HT 1"
DeviceDropDown1.AddItem "Device A: HT 2"
DeviceDropDown1.AddItem "Device A: HT 3"
DeviceDropDown1.AddItem "Device A: HT 4"
DeviceDropDown1.AddItem "Device A: HT 5"
DeviceDropDown1.AddItem "Device A: HT 6"
DeviceDropDown1.AddItem "Device A: HT 7"
DeviceDropDown1.AddItem "Device A: HT 8"
DeviceDropDown1.AddItem "Device B: HT 1"
DeviceDropDown1.AddItem "Device B: HT 2"
DeviceDropDown1.AddItem "Device B: HT 3"
DeviceDropDown1.AddItem "Device B: HT 4"
DeviceDropDown1.AddItem "Device B: HT 5"
DeviceDropDown1.AddItem "Device B: HT 6"
DeviceDropDown1.AddItem "Device B: HT 7"
DeviceDropDown1.AddItem "Device B: HT 8"
DeviceDropDown1.AddItem "Channel_Not_Available"
End If
End Sub
Private Sub HTNextButton_Click()
On Error Resume Next
DDi = 1
DDj = 1
Numberflag = 0
DeviceFlagA = 0
DeviceFlagB = 0
For DDi = 1 To 13
Device = Me.Controls.Item("DeviceDropDown" & DDi)
If Device = "Channel_Not_Available" Then
ElseIf Device = "Select Device" Then
MsgBox "Please select Number channel for Device" & DDi, vbCritical, "Error"
Exit For
Else
If InStr(1, Device, "Device A") Then
DeviceFlagA = 1
End If
If InStr(1, Device, "Device B") Then
DeviceFlagB = 1
End If
For DDj = 1 To 13
If DDi <> DDj Then
Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
If Device1 = "Channel_Not_Available" Then
Else
If Device = Device1 Then
MsgBox "Please select different number for Device" & DDj, vbCritical, "Error"
Numberflag = Numberflag + 1
Exit For
End If
End If
End If
Next
If Numberflag >= 1 Then
Exit For
End If
End If
Next
If DeviceFlagA = 1 Then
If HTSelection.DeviceSAInput.Text <> Empty Then
Else
MsgBox "Please enter valid number for device A", vbCritical, "Error"
End If
If DeviceFlagB = 1 Then
If HTSelection.DeviceSAInputB.Text <> Empty Then
Else
MsgBox "Please enter valid number for device B", vbCritical, "Error"
End If
If Numberflag = 0 Then
If (HTSelection.DeviceSAInput.Text <> Empty Or HTSelection.DeviceSAInputB <> Empty) Then
Number = HTSelection.DeviceSAInput.Text
Numberb = HTSelection.DeviceSAInputB.Text
Set clientNumber = CreateObject("Device.usb")
Set clientNumberb = CreateObject("Deviceb.usb")
End If
Me.Hide
Chart.Show
Else
MsgBox "Please enter valid number", vbCritical, "Error"
End If
End If
End Sub
这还不是答案,而是进一步调查的指标。我采用了您的 "FOR" 构造并对其进行了结构化,以便您可以看到块的开始位置和结束位置。从那里您可以看到,在您提供的代码部分,几乎所有 "End If" 以及 "Next Hti" 都丢失了。我想它们在那里,但如果不存在,那么错误的来源就很清楚了。
For Hti = 1 To 13
' If you want to check 2 dropdowns you must assign them to 2 different variables
Device = Me.Controls.Item("DeviceDropDown")
' Device is a Control object, so you will need Device.Name or Device.ItemsSelected depending what you want to check
' instead of "like" you can also use Instr for searching a string in another
If Device.ItemsSelected Like "*Device A*" And DeviceSAInput = Empty Then
MsgBox "Please enter address for Device A"
End If
If Device.ItemsSelected Like "*Device B*" And DeviceSAInputB = Empty Then
MsgBox "Please enter address for Device B"
Exit For
End If
' This part is missing in the code
Next Hti
请再次检查您的代码。最好提供完整的功能。像我的片段一样格式化代码并使用
Option Explicit
查找任何未定义的变量。
那我再看看问题
我接受了你的 Sub HTNextButton_Click() 并在 if-then-else 语句中做了一些更正。请检查我的评论,因为我不知道您的所有要求和条件。
Private Sub HTNextButton_Click()
Dim Numberflag As Boolean
' On Error Resume Next
' not required
' DDi = 1
' DDj = 1
Numberflag = False
DeviceFlagA = 0
DeviceFlagB = 0
For DDi = 1 To 13
Device = Me.Controls.Item("DeviceDropDown" & DDi)
If Device = "Select Device" Then
MsgBox "Please select Number channel for Device" & DDi, vbCritical, "Error"
Exit For
End If
If Device = "Channel_Not_Available" Then
' Do nothing (?)
Else
' Determine if it is Device A or B
If InStr(1, Device, "Device A") Then
DeviceFlagA = 1
End If
If InStr(1, Device, "Device B") Then
DeviceFlagB = 1
End If
' Check that the selection is not duplicate
For DDj = 1 To 13
If DDi <> DDj Then
Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
' The case of Device1 = "Channel_Not_Available" is also covered with the comparison
' as we should never get here with Device = "Channel_Not_Available"
' If duplicate entry found, leave the for-next loop DDj
If Device = Device1 Then
MsgBox "Please select different number for Device" & DDj, vbCritical, "Error"
Numberflag = True
Exit For
End If
End If
Next DDj
' if any of the entries is duplicate, leave the for-next loop DDi
If Numberflag Then
Exit For
End If
End If
Next DDi
' Do you need to execute the operations for "DeviceFlag" if Numberflag =True? What happens in case of "Select Device"?
If DeviceFlagA = 1 Then
If HTSelection.DeviceSAInput.Text = "" Then
MsgBox "Please enter valid number for device A", vbCritical, "Error"
Else
Number = HTSelection.DeviceSAInput.Text
End If
End If
If DeviceFlagB = 1 Then
If HTSelection.DeviceSAInputB.Text = "" Then
MsgBox "Please enter valid number for device B", vbCritical, "Error"
Else
Numberb = HTSelection.DeviceSAInputB.Text
End If
End If
If Numberflag = False Then
' I guess you only want those objects where the flag is set. consider moving them into the if statements above
If DeviceFlagA = 1 Then
Set clientNumber = CreateObject("Device.usb")
End If
If DeviceFlagB = 1 Then
Set clientNumberb = CreateObject("Deviceb.usb")
End If
Me.Hide
Chart.Show
Else
MsgBox "Please enter valid number", vbCritical, "Error"
End If
End Sub
https://i.stack.imgur.com/3D3xS.png 我有 13 个下拉菜单。 for 循环有效(检查以确保我不会 select 同一设备和号码不止一次)。尝试在最后解决 If 语句,验证当我 select 设备 A、B 或“通道不可用”时,两个文本输入是否有值。 1) 如果设备 A 是 selected,请验证第一个文本输入框中是否提供了数字。 2) 如果设备 B 是 selected,请验证是否为第二个文本输入框提供了数字。 3) 验证至少为设备 a 或 b 文本输入框提供了一个数字。目前,我的消息正在弹出,但是当我单击“下一步”按钮时,没有任何反应?即使我在文本输入中输入值并单击下一步,似乎也没有任何反应。我认为这是我的 If 语句的结构,需要一些帮助来创建验证。有什么想法吗?
If (HTSelection.DeviceDropDown1.List(0)) <> Empty Then
Else
DeviceDropDown1.AddItem "Device A: HT 1"
DeviceDropDown1.AddItem "Device A: HT 2"
DeviceDropDown1.AddItem "Device A: HT 3"
DeviceDropDown1.AddItem "Device A: HT 4"
DeviceDropDown1.AddItem "Device A: HT 5"
DeviceDropDown1.AddItem "Device A: HT 6"
DeviceDropDown1.AddItem "Device A: HT 7"
DeviceDropDown1.AddItem "Device A: HT 8"
DeviceDropDown1.AddItem "Device B: HT 1"
DeviceDropDown1.AddItem "Device B: HT 2"
DeviceDropDown1.AddItem "Device B: HT 3"
DeviceDropDown1.AddItem "Device B: HT 4"
DeviceDropDown1.AddItem "Device B: HT 5"
DeviceDropDown1.AddItem "Device B: HT 6"
DeviceDropDown1.AddItem "Device B: HT 7"
DeviceDropDown1.AddItem "Device B: HT 8"
DeviceDropDown1.AddItem "Channel_Not_Available"
End If
End Sub
Private Sub HTNextButton_Click()
On Error Resume Next
DDi = 1
DDj = 1
Numberflag = 0
DeviceFlagA = 0
DeviceFlagB = 0
For DDi = 1 To 13
Device = Me.Controls.Item("DeviceDropDown" & DDi)
If Device = "Channel_Not_Available" Then
ElseIf Device = "Select Device" Then
MsgBox "Please select Number channel for Device" & DDi, vbCritical, "Error"
Exit For
Else
If InStr(1, Device, "Device A") Then
DeviceFlagA = 1
End If
If InStr(1, Device, "Device B") Then
DeviceFlagB = 1
End If
For DDj = 1 To 13
If DDi <> DDj Then
Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
If Device1 = "Channel_Not_Available" Then
Else
If Device = Device1 Then
MsgBox "Please select different number for Device" & DDj, vbCritical, "Error"
Numberflag = Numberflag + 1
Exit For
End If
End If
End If
Next
If Numberflag >= 1 Then
Exit For
End If
End If
Next
If DeviceFlagA = 1 Then
If HTSelection.DeviceSAInput.Text <> Empty Then
Else
MsgBox "Please enter valid number for device A", vbCritical, "Error"
End If
If DeviceFlagB = 1 Then
If HTSelection.DeviceSAInputB.Text <> Empty Then
Else
MsgBox "Please enter valid number for device B", vbCritical, "Error"
End If
If Numberflag = 0 Then
If (HTSelection.DeviceSAInput.Text <> Empty Or HTSelection.DeviceSAInputB <> Empty) Then
Number = HTSelection.DeviceSAInput.Text
Numberb = HTSelection.DeviceSAInputB.Text
Set clientNumber = CreateObject("Device.usb")
Set clientNumberb = CreateObject("Deviceb.usb")
End If
Me.Hide
Chart.Show
Else
MsgBox "Please enter valid number", vbCritical, "Error"
End If
End If
End Sub
这还不是答案,而是进一步调查的指标。我采用了您的 "FOR" 构造并对其进行了结构化,以便您可以看到块的开始位置和结束位置。从那里您可以看到,在您提供的代码部分,几乎所有 "End If" 以及 "Next Hti" 都丢失了。我想它们在那里,但如果不存在,那么错误的来源就很清楚了。
For Hti = 1 To 13
' If you want to check 2 dropdowns you must assign them to 2 different variables
Device = Me.Controls.Item("DeviceDropDown")
' Device is a Control object, so you will need Device.Name or Device.ItemsSelected depending what you want to check
' instead of "like" you can also use Instr for searching a string in another
If Device.ItemsSelected Like "*Device A*" And DeviceSAInput = Empty Then
MsgBox "Please enter address for Device A"
End If
If Device.ItemsSelected Like "*Device B*" And DeviceSAInputB = Empty Then
MsgBox "Please enter address for Device B"
Exit For
End If
' This part is missing in the code
Next Hti
请再次检查您的代码。最好提供完整的功能。像我的片段一样格式化代码并使用
Option Explicit
查找任何未定义的变量。
那我再看看问题
我接受了你的 Sub HTNextButton_Click() 并在 if-then-else 语句中做了一些更正。请检查我的评论,因为我不知道您的所有要求和条件。
Private Sub HTNextButton_Click()
Dim Numberflag As Boolean
' On Error Resume Next
' not required
' DDi = 1
' DDj = 1
Numberflag = False
DeviceFlagA = 0
DeviceFlagB = 0
For DDi = 1 To 13
Device = Me.Controls.Item("DeviceDropDown" & DDi)
If Device = "Select Device" Then
MsgBox "Please select Number channel for Device" & DDi, vbCritical, "Error"
Exit For
End If
If Device = "Channel_Not_Available" Then
' Do nothing (?)
Else
' Determine if it is Device A or B
If InStr(1, Device, "Device A") Then
DeviceFlagA = 1
End If
If InStr(1, Device, "Device B") Then
DeviceFlagB = 1
End If
' Check that the selection is not duplicate
For DDj = 1 To 13
If DDi <> DDj Then
Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
' The case of Device1 = "Channel_Not_Available" is also covered with the comparison
' as we should never get here with Device = "Channel_Not_Available"
' If duplicate entry found, leave the for-next loop DDj
If Device = Device1 Then
MsgBox "Please select different number for Device" & DDj, vbCritical, "Error"
Numberflag = True
Exit For
End If
End If
Next DDj
' if any of the entries is duplicate, leave the for-next loop DDi
If Numberflag Then
Exit For
End If
End If
Next DDi
' Do you need to execute the operations for "DeviceFlag" if Numberflag =True? What happens in case of "Select Device"?
If DeviceFlagA = 1 Then
If HTSelection.DeviceSAInput.Text = "" Then
MsgBox "Please enter valid number for device A", vbCritical, "Error"
Else
Number = HTSelection.DeviceSAInput.Text
End If
End If
If DeviceFlagB = 1 Then
If HTSelection.DeviceSAInputB.Text = "" Then
MsgBox "Please enter valid number for device B", vbCritical, "Error"
Else
Numberb = HTSelection.DeviceSAInputB.Text
End If
End If
If Numberflag = False Then
' I guess you only want those objects where the flag is set. consider moving them into the if statements above
If DeviceFlagA = 1 Then
Set clientNumber = CreateObject("Device.usb")
End If
If DeviceFlagB = 1 Then
Set clientNumberb = CreateObject("Deviceb.usb")
End If
Me.Hide
Chart.Show
Else
MsgBox "Please enter valid number", vbCritical, "Error"
End If
End Sub