VBA Select case 1 to 100 only taking 1 - vba

I'm trying to make a select case that identifies if a number is lower than 0, 1 to 100 or greater than 100, the thing is that is just doesn't work. Here's my code:
If IsNumeric(TxtTemp.Text) Then
Select Case TxtTemp.Text
Case Is <= 0
TxtEstado.Text = "Solid"
Case 1 To 100
TxtEstado.Text = "Liquid"
Case Is > 100
TxtEstado.Text = "Gas"
End Select
Else
TxtEstado.Text = ""
End If
I know that this is an easy thing to do, the thing is that the select case returns liquid only if the number received is equal to 1. If it is lower or equal to 0 it returns solid, but if it is equal or greater to 2, it returns gas. I don't understand what I'm doing wrong.

Maybe it is easier to use a function for this kind of conversion
Function chText(txt As String) As String
On Error GoTo EH
Dim resTxt As String
If IsNumeric(txt) Then
Select Case CDbl(txt)
Case Is <= 0
resTxt = "Solid"
Case 1 To 100
resTxt = "Liquid"
Case Is > 100
resTxt = "Gas"
End Select
Else
resTxt = ""
End If
chText = resTxt
Exit Function
EH:
chText = "Error"
End Function
Sub Tester()
Debug.Print chText("101")
' TxtEstado.Text = chText(TxtTemp.Text)
End Sub

Related

Select Case statement and data validation

I am trying to validate data ranges using the select case statement. I am having issues with the other nested select cases. Is this possible or am i wishfull thinking? Or should i separate the select case statements to be stacked?
For instance this is my code in vb:
Select Case intyear
Case 2000 To 2025
Select Case intmonth
Case 1 To 12
BlnDateValid = True
End Select
Select Case intDay
Case 1 To 31
BlnDateValid = True
End Select
Select Case intHours
Case 0 To 23
BlnDateValid = True
End Select
Select Case intAddDays
Case 0 To 60
BlnDateValid = True
End Select
Select Case intAddHours
Case 0 To 23
BlnDateValid = True
End Select
Case Else
BlnDateValid = False
End Select
If blnDatevalid = false then
MessagebBox.Show("Please check all fields and enter valid
data", "Invalid data", MessageBoxButtons.OK)
Unfortunately, the indenting making sense doesn't help the code make sense. The whole point of Select Case is to neatly select one of multiple cases. A Select Case with one case is bad code and you should be using an If statement instead. You should especially be using a If statement in this case because you can replace all those Select Case statements with a single If statement.
If Not (intyear >= 2000 AndAlso intyear <= 2025 AndAlso
intmonth >= 1 AndAlso intmonth <= 12 AndAlso
intDay >= 1 AndAlso intDay <= 31 AndAlso
intHours >= 0 AndAlso intHours <= 23 AndAlso
intAddDays >= 0 AndAlso intAddDays <= 60 AndAlso
intAddHours >= 0 AndAlso intAddHours <= 23) Then
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If
All the Select or If checks will still leave you vulnerable to non-sensical values like February 30. Better to actually attempt to create a DateTime value.
Dim d As DateTime
Dim t As TimeSpan
Try
d = New DateTime(intYear, intMonth, intDay, intHours, 0, 0)
t = New TimeSpan(intAddDays, intAddHours, 0, 0)
If t > (New TimeSpan(60, 23, 0, 0)) Then Throw New ArgumentOutOfRangeException()
Catch
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End Try
or you can create a string and try parsing it:
If Not DateTime.TryParse($"{intYear}-{intMonth}-{intDay} {intHours}:00:00")
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If

VBA Select Case number to number greater than and less than

Sub ss()
Dim a As Double
a = 6.99999999
Select Case a
Case 0 To 7:
MsgBox "ok"
Case Else:
MsgBox "no"
End Select
End Sub
The Case 0 to 7 results in a check for a >= 0 and a <= 7. But what I want is a >= 0 and a < 7.
I also tried Case Is >=0, Is < 7.
How can I do this in a Select Case?
a = 6.99999999 should result in "ok"
a = 7 should result in "no"
select case true
case a >= 0 and a < 7
MsgBox "ok"
case else
MsgBox "no"
end select
But, unless you have more than two conditions, I would suggest you use an If instead.
Sub ss()
Dim a
a = 7
Select Case a
Case 7:
MsgBox "no"
Case 0 To 7:
MsgBox "ok"
Case Else:
MsgBox "no"
End Select
End Sub
Sub ss()
Dim a As Double
a = 6.99999999
Select Case a
Case 0 To 7:
If a = 7 Then
MsgBox "no"
Else
MsgBox "ok"
End If
Case Else:
MsgBox "no"
End Select
End Sub
I finally work out this solution. Thank you all!

convert a text to a number

I don't know why this code doesn't work ..i wanna just convert text to number .. It doesn't give me any error but it doesn't work
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Select Case chain
Case "1 - Très difficile"
ConvertCOMPLEXITYTToNumber = 1
Case "2 - Difficile"
ConvertCOMPLEXITYTToNumber = 2
Case "3 - Modérée"
ConvertCOMPLEXITYTToNumber = 3
Case "4 - Facile"
ConvertCOMPLEXITYTToNumber = 4
Case Else
ConvertCOMPLEXITYTToNumber = 0
End Select
Exit Function
End Function
That may be because you may have unwanted leading or trailing spaces which fails the comparison. Also you do not need Exit Function at the end of the code. It will exit any ways :)
Try this
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Dim Num As Integer
Select Case Trim(chain)
Case "1 - Très difficile": Num = 1
Case "2 - Difficile": Num = 2
Case "3 - Modérée": Num = 3
Case "4 - Facile": Num = 4
Case Else: Num = 0
End Select
ConvertCOMPLEXITYToNumber = Num
End Function
If IsNumeric(Left(Trim(chain),1)) Then
ConvertCOMPLEXITYToNumber = Left(Trim(chain),1)
Else
ConvertCOMPLEXITYToNumber = 0
End If
Here, my approach for you:
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
'Get the first character
chain = Left(Trim(chain), 1)
'If frist character is numeric
If IsNumeric(chain) Then
'If first number is less than 5, return value
If chain < 5 Then
ConvertCOMPLEXITYToNumber = CInt(chain)
End If
End If
End Function

convert hindi number to word in vb.net

i am trying to convert numbers to word for a hindi invoice .
like - 50001 to पांच हजार एक
Public Shared Function changeToWords(ByVal numb As [String]) As [String]
Dim val As [String] = "", wholeNo As [String] = numb, points As [String] = "", andStr As [String] = "", pointStr As [String] = ""
Dim endStr As [String] = ""
Try
Dim decimalPlace As Integer = numb.IndexOf(".")
If decimalPlace > 0 Then
wholeNo = numb.Substring(0, decimalPlace)
points = numb.Substring(decimalPlace + 1)
If Convert.ToInt32(points) > 0 Then
andStr = "दशाम्लौ"
' just to separate whole numbers from points
pointStr = translateCents(points)
End If
End If
val = [String].Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr)
Catch
End Try
Return val
End Function
Private Shared Function translateWholeNumber(ByVal number As [String]) As [String]
Dim word As String = ""
Try
Dim beginsZero As Boolean = False
'tests for 0XX
Dim isDone As Boolean = False
'test if already translated
Dim dblAmt As Double = (Convert.ToDouble(number))
'if ((dblAmt > 0) && number.StartsWith("0"))
If dblAmt > 0 Then
'test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0")
Dim numDigits As Integer = number.Length
Dim pos As Integer = 0
'store digit grouping
Dim place As [String] = ""
'digit grouping name:hundres,thousand,etc...
Select Case numDigits
Case 1
'ones' range
word = ones(number)
isDone = True
Exit Select
Case 2
'tens' range
word = tens(number)
isDone = True
Exit Select
Case 3
'hundreds' range
pos = (numDigits Mod 3) + 1
place = " सौ "
Exit Select
'thousands' range
Case 4, 5
pos = (numDigits Mod 4) + 1
place = " हजार "
Exit Select
Case 6, 7
pos = (numDigits Mod 6) + 1
place = " लाख "
Exit Select
'millions' range
Case 8, 9, 10
pos = (numDigits Mod 8) + 1
place = " करोड़ "
Exit Select
Case Else
'add extra case options for anything above Billion...
isDone = True
Exit Select
End Select
If Not isDone Then
'if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos))
'check for trailing zeros
If beginsZero Then
word = " " & word.Trim()
End If
End If
'ignore digit grouping names
If word.Trim().Equals(place.Trim()) Then
word = ""
End If
End If
Catch
End Try
Return word.Trim()
End Function
Private Shared Function tens(ByVal digit As [String]) As [String]
Dim digt As Integer = Convert.ToInt32(digit)
Dim name As [String] = Nothing
Select Case digt
Case 10
name = "दस"
Exit Select
Case 11
name = "ग्यारह"
Exit Select
Case 12
name = "बारह"
Exit Select
Case 13
name = "तेरह"
Exit Select
Case 14
name = "चौदह"
Exit Select
Case 15
name = "पंद्रह"
Exit Select
Case 16
name = "सोलह"
Exit Select
Case 17
name = "सतरह"
Exit Select
Case 18
name = "अट्ठारह"
Exit Select
Case 19
name = "उन्नीस"
Exit Select
Case 20
name = "बीस"
Exit Select
Case 21
name = "इक्कीस"
Exit Select
Case 22
name = "बाईस"
Exit Select
Case 23
name = "तेईस"
Exit Select
Case 24
name = "चौबीस"
Exit Select
Case 25
name = "पच्चीस"
Exit Select
Case 26
name = "छब्बीस"
Exit Select
Case 27
name = "सत्ताईस"
Exit Select
Case 28
name = "अट्ठाईस"
Exit Select
Case 29
name = "उनतीस"
Exit Select
Case 30
name = "तीस"
Exit Select
Case 31
name = "इकतीस"
Exit Select
Case 32
name = "बत्तीस"
Exit Select
Case 33
name = "तैंतीस"
Exit Select
Case 34
name = "चौंतीस"
Exit Select
Case 35
name = "पैंतीस"
Exit Select
Case 36
name = "छ्त्तीस"
Exit Select
Case 37
name = "सैंतीस"
Exit Select
Case 38
name = "अड़तीस"
Exit Select
Case 39
name = "उनतालीस"
Exit Select
Case 40
name = "चालीस"
Exit Select
Case 41
name = "इकतालीस"
Exit Select
Case 42
name = "बयालीस"
Exit Select
Case 43
name = "तैंतालीस"
Exit Select
Case 44
name = "चौंतालीस"
Exit Select
Case 45
name = "पैंतालीस"
Exit Select
Case 46
name = "छियालीस"
Exit Select
Case 47
name = "सैंतालीस"
Exit Select
Case 48
name = "अड़तालीस"
Exit Select
Case 49
name = "उनचास"
Exit Select
Case 50
name = "पचास"
Exit Select
Case 51
name = "इक्याबन"
Exit Select
Case 52
name = "बावन"
Exit Select
Case 53
name = "तिरेपन"
Exit Select
Case 54
name = "चौबन"
Exit Select
Case 55
name = "पचपन"
Exit Select
Case 56
name = "छप्पन"
Exit Select
Case 57
name = "सत्तावन"
Exit Select
Case 58
name = "अट्ठावन"
Exit Select
Case 59
name = "उनसठ"
Exit Select
Case 60
name = "साठ"
Exit Select
Case 61
name = "इकसठ"
Exit Select
Case 62
name = "बासठ"
Exit Select
Case 63
name = "तिरसठ"
Exit Select
Case 64
name = "चौंसठ"
Exit Select
Case 65
name = "पैंसठ"
Exit Select
Case 66
name = "छियासठ"
Exit Select
Case 67
name = "सड़सठ"
Exit Select
Case 68
name = "अड़सठ"
Exit Select
Case 69
name = "उनहत्तर"
Exit Select
Case 70
name = "सत्तर"
Exit Select
Case 71
name = "इकहत्तर"
Exit Select
Case 72
name = "बहत्तर"
Exit Select
Case 73
name = "तिहत्तर"
Exit Select
Case 74
name = "चौहत्तर"
Exit Select
Case 75
name = "पचहत्तर"
Exit Select
Case 76
name = "छिहत्तर"
Exit Select
Case 77
name = "सतहत्तर"
Exit Select
Case 78
name = "अठहत्तर"
Exit Select
Case 79
name = "उनासी"
Exit Select
Case 80
name = "अस्सी"
Exit Select
Case 81
name = "इक्यासी"
Exit Select
Case 82
name = "बयासी"
Exit Select
Case 83
name = "तिरासी"
Exit Select
Case 84
name = "चौरासी"
Exit Select
Case 85
name = "पचासी"
Exit Select
Case 86
name = "छियासी"
Exit Select
Case 87
name = "सतासी"
Exit Select
Case 88
name = "अठासी"
Exit Select
Case 89
name = "नवासी"
Exit Select
Case 90
name = "नब्बे"
Exit Select
Case 91
name = "इक्यानवे"
Exit Select
Case 92
name = "बानवे"
Exit Select
Case 93
name = "तिरानवे"
Exit Select
Case 94
name = "चौरानवे"
Exit Select
Case 95
name = "पचानवे"
Exit Select
Case 96
name = "छियानवे"
Exit Select
Case 97
name = "सत्तानवे"
Exit Select
Case 98
name = "अट्ठानवे"
Exit Select
Case 99
name = "निन्यानवे"
Exit Select
Case Else
If digt > 0 Then
name = (tens(digit.Substring(0, 1) & "0") & " ") + ones(digit.Substring(1))
End If
Exit Select
End Select
Return name
End Function
Private Shared Function ones(ByVal digit As [String]) As [String]
Dim digt As Integer = Convert.ToInt32(digit)
Dim name As [String] = ""
Select Case digt
Case 1
name = "एक"
Exit Select
Case 2
name = "दो"
Exit Select
Case 3
name = "तीन"
Exit Select
Case 4
name = "चार"
Exit Select
Case 5
name = "पांच"
Exit Select
Case 6
name = "छः"
Exit Select
Case 7
name = "सात"
Exit Select
Case 8
name = "आठ"
Exit Select
Case 9
name = "नौ"
Exit Select
End Select
Return name
End Function
Private Shared Function translateCents(ByVal cents As [String]) As [String]
Dim cts As [String] = "", digit As [String] = "", engOne As [String] = ""
For i As Integer = 0 To cents.Length - 1
digit = cents(i).ToString()
If digit.Equals("0") Then
engOne = "शून्य"
Else
engOne = ones(digit)
End If
cts += " " & engOne
Next
Return cts
End Function
i tried the above coe . it is running but when
the number comes like 50001 it gives output पचास हजार सौ एक .
i want to write this as पचास हजार एक .
This modification seems to be working for me:
'if transalation is not done, continue...(Recursion comes in now!!)
If (number.Substring(0, 1) = "0") Then
word = translateWholeNumber(number.Substring(pos))
Else
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos))
End If
Edit: Doing this breaks output if the original string sent to changeToWords begins with any zeros. To rectify that condition, you can trim leading zeros, before the string is processed:
wholeNo As [String] = numb.TrimStart("0"c)

Need help converting a number to words

I am trying to convert a number to a word from a RDLC report:
Public Shared Function changeToWords(ByVal numb As [String]) As [String]
Dim val As [String] = ""
Dim wholeNo As [String] = numb
Dim points As [String] = ""
Dim andStr As [String] = ""
Dim pointStr As [String] = ""
Dim endStr As [String] = ""
Try
Dim decimalPlace As Integer = numb.IndexOf(".")
If decimalPlace > 0 Then
wholeNo = numb.Substring(0, decimalPlace)
points = numb.Substring(decimalPlace + 1)
If Convert.ToInt32(points) > 0 Then
andStr = "point"
' just to separate whole numbers from points
pointStr = translateCents(points)
End If
End If
val = [String].Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr)
Catch
End Try
Return val
End Function
Private Shared Function translateWholeNumber(ByVal number As [String]) As [String]
Dim word As String = ""
Try
Dim beginsZero As Boolean = False
'tests for 0XX
Dim isDone As Boolean = False
'test if already translated
Dim dblAmt As Double = (Convert.ToDouble(number))
'if ((dblAmt > 0) && number.StartsWith("0"))
If dblAmt > 0 Then
'test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0")
Dim numDigits As Integer = number.Length
Dim pos As Integer = 0
'store digit grouping
Dim place As [String] = ""
'digit grouping name:hundres,thousand,etc...
Select Case numDigits
Case 1
'ones' range
word = ones(number)
isDone = True
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 2
'tens' range
word = tens(number)
isDone = True
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 3
'hundreds' range
pos = (numDigits Mod 3) + 1
place = " Hundred "
Exit Select
' TODO: might not be correct. Was : Exit Select
'thousands' range
Case 4, 5, 6
pos = (numDigits Mod 4) + 1
place = " Thousand "
Exit Select
' TODO: might not be correct. Was : Exit Select
'millions' range
Case 7, 8, 9
pos = (numDigits Mod 7) + 1
place = " Million "
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 10
'Billions's range
pos = (numDigits Mod 10) + 1
place = " Billion "
Exit Select
Case Else
' TODO: might not be correct. Was : Exit Select
'add extra case options for anything above Billion...
isDone = True
Exit Select
' TODO: might not be correct. Was : Exit Select
End Select
If Not isDone Then
'if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos))
'check for trailing zeros
If beginsZero Then
word = " and " & word.Trim()
End If
End If
'ignore digit grouping names
If word.Trim().Equals(place.Trim()) Then
word = ""
End If
End If
Catch
End Try
Return word.Trim()
End Function
Private Shared Function tens(ByVal digit As [String]) As [String]
Dim digt As Integer = Convert.ToInt32(digit)
Dim name As [String] = Nothing
Select Case digt
Case 10
name = "Ten"
Exit Select
' TODO: might not be correct. Was : Exit Select \
Case 11
name = "Eleven"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 12
name = "Twelve"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 13
name = "Thirteen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 14
name = "Fourteen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 15
name = "Fifteen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 16
name = "Sixteen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 17
name = "Seventeen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 18
name = "Eighteen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 19
name = "Nineteen"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 20
name = "Twenty"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 30
name = "Thirty"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 40
name = "Fourty"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 50
name = "Fifty"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 60
name = "Sixty"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 70
name = "Seventy"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 80
name = "Eighty"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 90
name = "Ninety"
Exit Select
Case Else
' TODO: might not be correct. Was : Exit Select
If digt > 0 Then
name = (Convert.ToString(tens(digit.Substring(0, 1) & "0")) & " ") & Convert.ToString(ones(digit.Substring(1)))
End If
Exit Select
' TODO: might not be correct. Was : Exit Select
End Select
Return name
End Function
Private Shared Function ones(ByVal digit As [String]) As [String]
Dim digt As Integer = Convert.ToInt32(digit)
Dim name As [String] = ""
Select Case digt
Case 1
name = "One"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 2
name = "Two"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 3
name = "Three"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 4
name = "Four"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 5
name = "Five"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 6
name = "Six"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 7
name = "Seven"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 8
name = "Eight"
Exit Select
' TODO: might not be correct. Was : Exit Select
Case 9
name = "Nine"
Exit Select
' TODO: might not be correct. Was : Exit Select
End Select
Return name
End Function
Private Shared Function translateCents(ByVal cents As [String]) As [String]
Dim cts As [String] = ""
Dim digit As [String] = ""
Dim engOne As [String] = ""
For i As Integer = 0 To cents.Length - 1
digit = cents(i).ToString()
If digit.Equals("0") Then
engOne = "Zero"
Else
engOne = ones(digit)
End If
cts += " " & engOne
Next
Return cts
End Function
I am getting the wrong output from the conversion.
For 52001 the given output is Fifty Two Thousand and Hundred One.
However, it should be Fifty Two Thousand and One.
You'll need to add a conditional statement to modify your string concatenation behavior when the beginning of your substring is a zero.
'if transalation is not done, continue...(Recursion comes in now!!)
If (number.Substring(0, 1) = "0") Then
word = translateWholeNumber(number.Substring(pos))
Else
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos))
End If
Edit: Doing this breaks output if the original string sent to changeToWords begins with any zeros. To rectify that condition, you can trim leading zeros, before the string is processed:
Dim wholeNo As [String] = numb.TrimStart("0"c)
Just change this line:
If Not isDone Then
'if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos))
'check for trailing zeros
If beginsZero Then
word = translateWholeNumber(number.Substring(0, pos))+ " and " + translateWholeNumber(number.Substring(pos))
End If
End If