Vba Visual Basic,IF语句中的数字范围
我将如何在VisualBasic中编写这行c#。我试图从用户那里获得一个输入并提供一个结果,因为输入值介于一个数字范围之间Vba Visual Basic,IF语句中的数字范围,vba,Vba,我将如何在VisualBasic中编写这行c#。我试图从用户那里获得一个输入并提供一个结果,因为输入值介于一个数字范围之间 if int(>65 || <=73) { } 像这样: If HbInt > 65 And HbInt <= 73 Then ... End If 如果HbInt>65和HbInt为 vba中不允许使用此Dim Hb As String=txtInput1.Text,我假设txtInput1是单元格范围的命名引用 你必须写在下面 Dim Hb
if int(>65 || <=73)
{
}
像这样:
If HbInt > 65 And HbInt <= 73 Then
...
End If
如果HbInt>65和HbInt为
vba中不允许使用此Dim Hb As String=txtInput1.Text
,我假设txtInput1
是单元格范围的命名引用
你必须写在下面
Dim Hb As String:Hb=txtInput1.Text
另外,这个dimhbint作为Integer=Integer.Parse(Hb)
也不正确
正确的方法是:
Dim HbInt作为整数: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
Sub NumRange()
将Hb标注为字符串:Hb=txtInput1.Text
如果是数字(Hb),则
作为整数的尺寸HbInt:HbInt=CInt(Hb)
如果HbInt>65且HbInt只是扩展@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
你是说VB.NET还是VBA?这两者可能完全不同——例如,VBA没有Integer.Parse,但VB.NET有。问题需要正确标记…这会产生与TXInput1相关的错误,请在完整示例中定义它您的示例会出现错误“运行时错误424;需要对象”,请修复它
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