2016-04-23 96 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