2016-04-23 58 views
1

我将如何在Visual Basic中编写这行c#。即时通讯尝试从用户获得输入并提供结果,因为输入落在数字范围之间。Visual Basic,IF语句中的数字范围

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 
+0

你的意思是VB.NET或VBA?这两者可能完全不同 - 例如,VBA没有Integer.Parse,但VB.NET的确如此。该问题需要正确标记... –

回答

0

像这样:

If HbInt > 65 And HbInt <= 73 Then 
... 
End If 
2

对于Reference See this

这个Dim Hb As String = txtInput1.Text不允许在vba中,我假设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 
1

只要扩展@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