在 IF 语句中使用数字范围的 Visual Basic
在 IF 语句中使用数字范围的 Visual Basic
我应该如何在Visual Basic中编写这行C#代码。我正在尝试从用户那里获取输入并提供结果,只要输入在数字范围内。
if int(>65 || <=73) { }
这是我到目前为止的代码。
Dim Hb As String = txtInput1.Text If IsNumeric(Hb) Then Dim HbInt As Integer = Integer.Parse(Hb) Else Output("The Hb value needs to be numeric") End If
admin 更改状态以发布 2023年5月22日
我想对@NewGuy提供的答案进行进一步扩展,我更愿意使用Select Case
语句来评估提供的数字。这将允许更多的选项:
Option Explicit Sub tmpTest() Dim strHB As String strHB = InputBox("Give me a number between 1 and 100", "Your choice...") If IsNumeric(strHB) Then Select Case CLng(strHB) Case 66 To 73 MsgBox "You picked my range!" Case 1 To 9 MsgBox "One digit only? Really?" Case 99 MsgBox "Almost..." Case Else MsgBox "You selected the number " & strHB End Select Else MsgBox "I need a number and not this:" & Chr(10) & Chr(10) & " " & strHB & Chr(10) & Chr(10) & "Aborting!" End If End Sub
参考链接:请参考这里。
在vba中,Dim Hb As String = txtInput1.Text
是不允许的,我假设txtInput1
是对单元格区域的命名引用。
你需要这样写:Dim Hb As String: Hb = txtInput1.Text
此外,Dim HbInt As Integer = Integer.Parse(Hb)
也是不正确的。
正确的方式是:
Dim HbInt As Integer: HbInt = CInt(Hb)
所以你需要的代码是:
Sub NumRange() Dim Hb As String: Hb = txtInput1.Text if IsNumeric(Hb) then Dim HbInt As Integer: HbInt = CInt(Hb) if HbInt > 65 And HbInt <=73 then Do things...... Else Msgbox "Number Entered is out of Range" End if Else Msgbox "Invalid Input." End if End Sub