I'm creating a DnD Character Creation program, and I've gotten myself stuck at the "Experience / Level" area. What I want is for every 1000 experience, the level to go up (so 0 to 999 is level 0,
So, I've used the following code to get where I am, but it doesn't change the label (lblLevel) to 1 when experience (txtExperience) is changed to 1000 (or 2 when experience is changed to 2000).
Private Sub txtExperience_textChanged(sender As Object, e As EventArgs) Handles txtExperience.TextChanged
If txtExperience.Text = letters Then
lblLevel.Text = "0"
ElseIf txtExperience.Text >= 10000 Then
lblLevel.Text = "Maxed"
End If
Select Case txtExperience.Text
Case Is <= "999"
lblLevel.Text = "0"
Case "1000" To "1999"
lblLevel.Text = "1"
Case "2000" To "2999"
lblLevel.Text = "2"
Case "3000" To "3999"
lblLevel.Text = "3"
Case "4000" To "4999"
lblLevel.Text = "4"
Case "5000" To "5999"
lblLevel.Text = "5"
Case "6000" To "6999"
lblLevel.Text = "6"
Case "7000" To "7999"
lblLevel.Text = "7"
Case "8000" To "8999"
lblLevel.Text = "8"
Case "9000" To "9999"
lblLevel.Text = "9"
End Select
End Sub
I'll be honest, I'm not sure if I'm using Select Case ... correctly, and when I tried using an If Statement (and Else If in place each of the Case ... to ...) it wouldn't work either. Any help would be greatly appreciated.
You could simplify that even further. Using basic math you can get rid of the long Select Case statement:
Dim Experience As Integer = 0
If Integer.TryParse(txtExperience.Text, Experience) = True Then
If Experience >= 10000 Then
lblLevel.Text = "Maxed"
Return
End If
lblLevel.Text = Math.Floor(Experience / 1000).ToString()
Else
MessageBox.Show("Input must be a whole number between 0 and 10000", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Here's an online test of my code: https://dotnetfiddle.net/VtGFLx
There is a difference between integer and string
Select Case Integer.Parse(txtExperience.Text)
Case Is <= 999
lblLevel.Text = 0
Case 1000 To 1999
lblLevel.Text = 1
Case 2000 To 2999
lblLevel.Text = 2
Case 3000 To 3999
lblLevel.Text = 3
Case 4000 To 4999
lblLevel.Text = 4
Case 5000 To 5999
lblLevel.Text = 5
Case 6000 To 6999
lblLevel.Text = 6
Case 7000 To 7999
lblLevel.Text = 7
Case 8000 To 8999
lblLevel.Text = 8
Case 9000 To 9999
lblLevel.Text = 9
End Select
Related
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
I try a select case between number. Each 150000 the code the Textbox5 change on click button.
Select Case TextBox5.Text
Case 0 To 150000
TextBox6.Text = "-"
Case 150001 To 300001
TextBox6.Text = "+1-"
Case 300002 To 450002
TextBox6.Text = "+2-"
Case 450003 To 600003
TextBox6.Text = "+3-"
Case 600004 To 750004
TextBox6.Text = "+4-"
Case 750005 To 900005
TextBox6.Text = "+5-"
Case 900006 To 1050006
TextBox6.Text = "+6-"
Case Else
TextBox6.Text = "+Extra-"
End Select
When I try any number between 900006 to 1050006 I have "+Extra-" not "+6-"
and try over 1050006 I have "-"
This is interesting. But before I try to find out what it's causing, I provide you the correct way to do this: a String is not a number, set Option Strict to On and learn to use type safe code. Don't let the compiler guess what you're trying to achieve.
You can use Int32.TryParse:
Dim number As Int32
If Not Int32.TryParse(TextBox5.Text, number) Then
MessageBox.Show("Please enter a valid integer")
Return
End If
Select Case number ' now integer is the target type
Case 0 To 150000
TextBox6.Text = "-"
Case 150001 To 300001
TextBox6.Text = "+1-"
Case 300002 To 450002
TextBox6.Text = "+2-"
Case 450003 To 600003
TextBox6.Text = "+3-"
Case 600004 To 750004
TextBox6.Text = "+4-"
Case 750005 To 900005
TextBox6.Text = "+5-"
Case 900006 To 1050006
TextBox6.Text = "+6-"
Case Else
TextBox6.Text = "+Extra-"
End Select
This will work and compile also with Option Strict On.
Now why your code doesn't work. If you change the last range to Case 900006 To 999999 it will work as expected. This has to do how strings are compared. Even if this compiles (Strict Off) the Case will treat the range 900006 To 1050006 as strings so as "900006" To "1050006", so they are compared letter for letter from left to right, sinvce "9" is "greater" than "1" this condition is never true, that's why you never get into the last Case but into the Case Else.
Documentation:
The expressions in expressionlist can be of any data type, provided
they are implicitly convertible to the type of testexpression and the
appropriate comparison operator is valid for the two types it is being
used with.
With Option Strict Off the expressionlist (the range) is converted to the type of testexpression which is String (because of TextBox5.Text).
With Option Strict On you correctly get a compiler error because a String is not an Integer:
Option Strict On disallows implicit conversions from 'Integer' to
'String'
I agree with Tim Schmelter. Complementing its answer you should use
Select Case CLng(TextBox5.Text)
See https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions for details and while you are at it avoid ids like TextBox5
Here is another way to do this, and it also works if using ASP Classic, which does not support the "To" operator. This code also shows the use of a nested Select/Case structure, within the first Select/Case:
CardName = Left(CardNumber, 4)
Select Case CardName
Case 1800
GetCardName = "JCB (Japanese Credit Bureau)"
Case 2014
GetCardName = "enRoute"
Case 2131
GetCardName = "JCB (Japanese Credit Bureau)"
Case 2149
GetCardName = "enRoute"
'Case 3000 To 3059:
Case (CardName => 3000 And CardName <= 3059)
GetCardName = "Diners Club"
'Case 3400 To 3499
Case (CardName => 3400 And CardName <= 3499)
GetCardName = "American Express"
'Case 3528 To 3589
Case (CardName => 3528 And CardName <= 3589)
GetCardName = "JCB (Japanese Credit Bureau)"
'Case 3600 To 3699
Case (CardName => 3600 And CardName <= 3699)
GetCardName = "Diners Club"
'Case 3700 To 3799
Case (CardName => 3700 And CardName <= 3799)
GetCardName = "American Express"
'Case 3800 To 3889
Case (CardName => 3800 And CardName <= 3889)
GetCardName = "Diners Club"
'Case 3890 To 3899
Case (CardName => 3890 And CardName <= 3899)
GetCardName = "carteBlanche"
'Case 4000 To 4999
Case (CardName => 4000 And CardName <= 4999)
GetCardName = "VISA"
'Case 5100 To 5599
Case (CardName => 5100 And CardName <= 5599)
GetCardName = "MasterCard"
Case 5610
GetCardName = "Australian BankCard"
Case 6011
GetCardName = "Discover"
Case Else
Select Case Left(CardNumber, 1)
Case 1 'Airline
GetCardName = "Other (Airline)"
Case 2 'Airline and other industry assignments
GetCardName = "Other (Airline and other idustries)"
Case 3 'Travel/Entertainment
GetCardName = "Other (Travel/Entertainment)"
Case 4 'Banking and financial
GetCardName = "Other (Banking/Financial)"
Case 5 'Banking and financial
GetCardName = "Other (Banking/Financial)"
Case 6 'Merchandising and banking
GetCardName = "Other (Merchandising/Banking)"
Case 7 'Petroleum
GetCardName = "Other (Gas Company)"
Case 8 'HealthCare/Telecommunications and other
GetCardName = "Other (HealthCare/Telecomm/Etc)"
Case 9 'National assignment
GetCardName = "Other [Card Carrier Unknown]"
Case 0 'ISO/TC 68 and other industry assignments
GetCardName = "Other [Card Carrier Unknown]"
End Select
End Select
Dim i as Long
If Long.TryParse(TextBox5.Text, i)
Select Case i
Case 0 To 150000
TextBox6.Text = "-"
Case 150001 To 300001
TextBox6.Text = "+1-"
Case 300002 To 450002
TextBox6.Text = "+2-"
Case 450003 To 600003
TextBox6.Text = "+3-"
Case 600004 To 750004
TextBox6.Text = "+4-"
Case 750005 To 900005
TextBox6.Text = "+5-"
Case 900006 To 1050006
TextBox6.Text = "+6-"
Case Else
TextBox6.Text = "+Extra-"
End Select
Else
TextBox6.Text = "Not a Number"
End If
Modify to your requirements.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to create a tool using macro so that when few criteria are selected, a certain rating will be shown. There are three criteria that need to be filtered before getting the result.
For example, if the industry (eg. Agriculture) is selected, followed by selection of countries (e.g Indonesia), followed by selection of certain ratio (e.g. 2.5), a rating from 1 to 6 will be given (in this case, 3).
I tried the following code, nothing appears under my rating column but there is no error message. Is there anything missing in my code which causes no result happening?
Private Sub CommandButton1_Click()
If Range("V4").Value = "A.Agriculture,forestry and fishing" Then
If Range("W4").Value = All Or ID Or SG Then
If Range("D4").Value <= 0 Then
Range("X4").Value = 6
ElseIf Range("M4").Value > 4 Then
Range("X4").Value = 5
ElseIf Range("M4").Value <= 4 And Value > 2 Then
Range("X4").Value = 4
ElseIf Range("M4").Value <= 2 And Value > 1 Then
Range("X4").Value = 3
ElseIf Range("M4").Value <= 1 And Value > 0 Then
Range("X4").Value = 2
ElseIf Range("M4").Value <= 0 Then
Range("X4").Value = 1
End If
ElseIf Range("W4").Value = MY Or TH Then
If Range("D4").Value <= 0 Then
Range("X4").Value = 6
ElseIf Range("M4").Value > 4.5 Then
Range("X4").Value = 5
ElseIf Range("M4").Value <= 4.5 And Value > 2 Then
Range("X4").Value = 4
ElseIf Range("M4").Value <= 2 And Value > 1 Then
Range("X4").Value = 3
ElseIf Range("M4").Value <= 1 And Value > 0 Then
Range("X4").Value = 2
ElseIf Range("M4").Value <= 0 Then
Range("X4").Value = 1
End If
End If
End If
End Sub
Your If statements with Or are not properly formed. Additionally, a combination of If and Select Case statements would seem to be best for these multiple, nested conditions. This should also make your code mode readable.
If Range("W4").Value = "All" Or Range("W4").Value = "ID" Or Range("W4").Value = "SG" Then
VBA is case sensitive by default and strings need to be enclosed in quotes; e.g. All <> "All" and "ALL" <> "all". Best to use the LCase and UCase functions to compare "Apples" with "Apples".
Private Sub CommandButton1_Click()
If Range("V4").Value = "A.Agriculture,forestry and fishing" Then
Select Case LCase(Range("W4").Value)
Case "all", "id", "sg"
If Range("D4").Value <= 0 Then
Range("X4").Value = 6
Else
Select Case Range("M4").Value
Case Is > 4
Range("X4").Value = 5
Case 2.01 To 4
Range("X4").Value = 4
Case 1.01 To 2
Range("X4").Value = 3
Case 0.01 To 1
Range("X4").Value = 2
Case Is <= 0
Range("X4").Value = 1
Case Is > 4
Range("X4").Value = 6
End Select
End If
Case "my", "th"
If Range("D4").Value <= 0 Then
Range("X4").Value = 6
Else
Select Case Range("M4").Value
Case Is > 4.5
Range("X4").Value = 5
Case 2.01 To 4.5
Range("X4").Value = 4
Case 1.01 To 2
Range("X4").Value = 3
Case 0.01 To 1
Range("X4").Value = 2
Case Is <= 0
Range("X4").Value = 1
Case Is > 4
Range("X4").Value = 6
End Select
End If
End Select
End If
End Sub
Note that I have added a small decimal value to the between style Case statements in order to achieve the greater than and less than or equal to logic.
Add an Else case at the very end to handle errors. Chances are your If... ElseIf block isn't actually doing anything.
Also, because you are checking so many CASES, this looks like a great opportunity to re-structure your If... Then block to a Select... Case block..
Select [ Case ] testexpression 'stuff here
[ Case expression1 ]
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
more info - https://msdn.microsoft.com/en-us/library/cy37t14y.aspx for info.
I am trying to write some code that says if textbox1 is equal to 0 between 10 then HandDecimal = 1. Else if textbox1 is equal to 10.1 through 100, then HandDecimal = 3. Else if textbox1 is equal to 100.1 and greater than HandDecimal = 5.
Here is my code, but it does not seem to work for me.
If WeightDecimal = 0 <= 10 Then
HandDecimal = 1
ElseIf WeightTextBox.Text = 10 <= 100 Then
HandDecimal = 3
ElseIf WeightTextBox.Text >= 100.1 Then
HandDecimal = 5
End If
How do I have to change the code to make it work?
Dim weight as Decimal = Decimal.Parse(WeightTextBox.Text)
If weight >= 0 AndAlso weight <= 10 Then
HandDecimal = 1
ElseIf weight > 10 AndAlso weight <= 100 Then
HandDecimal = 3
ElseIf weight > 100 Then
HandDecimal = 5
End If
Select Case statement with To operator
Select Case WeightDecimal
Case 0 To 10
HandDecimal = 1
Case 10.1 To 100
HandDecimal = 3
Case Else
HandDecimal = 5
End Select
I'm working on an assignment where the program needs to calculate and display the total cost for shipping a package, based on weight and whether it is being shipped to Continental U.S., Alaska, or Hawaii. When I click the Calculate button, though, the label that is supposed to display the total is left blank. I've looked through this and tried placing the calculation in different parts/at the end of the "If" statements. Here's what I have thus far, any help would be appreciated:
Dim decWeight As Decimal
Dim decTotalCost As Decimal
Dim decDestination As Decimal
Dim decRate As Decimal
If IsNumeric(txtWeight.Text) Then
decWeight = Convert.ToDecimal(txtWeight.Text)
If decWeight <= 30 > 0 Then
If radContinental.Checked Then
decDestination = 1
ElseIf radHawaii.Checked Then
decDestination = 1.2
ElseIf radAlaska.Checked Then
decDestination = 1.26
End If
If decWeight <= 2 Then
decRate = 3.69
ElseIf decWeight <= 4 > 2 Then
decRate = 4.86
ElseIf decWeight <= 6 > 4 Then
decRate = 5.63
ElseIf decWeight <= 8 > 6 Then
decRate = 5.98
ElseIf decWeight <= 10 > 8 Then
decRate = 6.28
ElseIf decWeight <= 30 > 10 Then
decRate = 15.72
End If
decTotalCost = decRate * decDestination
lblTotalCost.Text = decTotalCost.ToString("C")
ElseIf decWeight <= 0 Then
MsgBox("Please Enter a Positive Weight.", , "Input Error")
ElseIf decWeight > 30 Then
MsgBox("You Have Entered " & decWeight.ToString() & ". Please Enter a Weight Under 30 Pounds", , "Input Error")
End If
ElseIf txtWeight.Text = "" Then
MsgBox("Please Enter the Weight", , "Input Error")
Else : MsgBox("Please Enter a Number", , "Input Error")
End If
You should try this if statement: If decWeight <= 30 and decWeight > 0 Then
This will check if the decWeight is less than or equal to 30 and make sure that it is 'non-zero' Hope this helps :-)