2

how would i write this line of c# in visual basic. im trying to get a input from the user and provide a result, given the input falls between a number range.

if int(>65 || <=73)
{

}

This is the code i have so far.

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
Chamath Perera
  • 21
  • 1
  • 1
  • 2
  • Do you mean VB.NET or VBA? The two can be quite different - VBA does not have Integer.Parse, for example, but VB.NET does. The question needs to be tagged correctly... – Cindy Meister Apr 24 '16 at 08:18
  • This gives error, related to txtInput1, please define it in full example – WebComer Dec 08 '18 at 09:47

3 Answers3

2

For Reference See this.

This Dim Hb As String = txtInput1.Text is not allowed in vba and I assume txtInput1 is a named reference to a cell range.

You have to write it as below Dim Hb As String: Hb = txtInput1.Text

Also this Dim HbInt As Integer = Integer.Parse(Hb) isn't right as well

The right way would be:

Dim HbInt As Integer: HbInt = CInt(Hb)

So the code for your need would be:

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
Community
  • 1
  • 1
Stupid_Intern
  • 3,382
  • 8
  • 37
  • 74
1

Just expanding upon the answer provided by @NewGuy I'd rather use the Select Case statement to evaluate the number provided. This will allow for more options:

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
Ralph
  • 9,284
  • 4
  • 32
  • 42
0

Like this:

If HbInt > 65 And HbInt <= 73 Then
...
End If
ttaaoossuuuu
  • 7,786
  • 3
  • 28
  • 58