Calculation Issue in VB 2010 - vb.net

I am having trouble figuring out where my issue with this code is. My calculations work perfectly until the last case (CASE IS > 8), which continuously returns a 0.00 as the result. I'm sure its something small that a newbie like me is missing due to lack of experience. Thank you for your help!
' Declaration of Variable
Convert.ToInt32(txtAttending.Text)
Dim decAttending = txtAttending.Text
If IsNumeric(txtAttending.Text) And txtAttending.Text <= 16 Then
Select Case txtAttending.Text
Case Is = 1
decCost = 695 * decAttending
Case 2 To 4
decCost = 545 * decAttending
Case 5 To 8
decCost = 480 * decAttending
Case Is > 8
decCost = 395 * decAttending
End Select
Else
MsgBox("Please double check that your input is a number not greater than 16", , "Input Error")
End If
If radYes.Checked = True Then
decFinalCost = (decCost - (decCost * 0.15))
lblRepeatDiscount.Visible = True
decDiscount = (decCost * 0.15)
lblDiscount.Text = decDiscount.ToString("C")
lblTotalPrice.Text = decFinalCost.ToString("C")
Else
decFinalCost = decCost
lblTotalPrice.Text = decFinalCost.ToString("C")
End If

Convert.ToInt32(txtAttending.Text) does not convert the text in-place, as your first line of code seems to assert; this is a no-op. txtAttending.Text has type System.String, and will always have that type.
Your case statement should look more like this:
If IsNumeric(txtAttending.Text) Then
Dim decAttending = Convert.ToInt32(txtAttending.Text)
Select Case decAttending
Case Is = 1
decCost = 695 * decAttending
Case 2 To 4
decCost = 545 * decAttending
Case 5 To 8
decCost = 480 * decAttending
Case 9 To 16
decCost = 395 * decAttending
End Select
Else
MsgBox("Please double check that your input is a number not greater than 16", , "Input Error")
This version always uses the System.Int32 variable decAttending when doing numeric operations.

Case Else
decCost = 395 * decAttending

Shouldn't you convert the text to a number first? I'd change these lines:
If IsNumeric(txtAttending.Text) And Val(txtAttending.Text) <= 16 Then
Select Case Val(txtAttending.Text)
Notice that this snippet has no effect: Convert.ToInt32(txtAttending.Text), you're converting a text to an integer, but the result is not being stored in a variable.
Also, what happens if the number is <= 0 ?

Apart from mentioned advices, it seems great, but
are you sure that all code you did is written, or is there a thrown code that manipulate
Diccost,or disattending,please assure us that.

Related

VBA Select case 1 to 100 only taking 1

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

getting a return value from a function into another sub routine

I have the below code which returns the premium amount but i am not sure how to get this to display in another function. The sub routine i want to get the value into just displays text about the options selected then i need to display the value calculated based on those results.
Function ProcessClaims(ClaimsList As claimsList, PremiumIn As Decimal) As Decimal
Dim adjustedPremium, originalPremium As Decimal
Dim declined As Decimal
originalPremium = ClaimsList.claimValue * 100 \ 5
If ClaimsList.claimValue <= 5000 And ClaimsList.isPersonalInjury = False Then
adjustedPremium = originalPremium
ElseIf ClaimsList.claimValue > 5000 And ClaimsList.claimValue <= 10000 And ClaimsList.isPersonalInjury = False Then
adjustedPremium = originalPremium * 100 / 10
ElseIf ClaimsList.claimValue > 10000 Or ClaimsList.isPersonalInjury = False Then
adjustedPremium = -1.0
declined = -1.0
End If
If adjustedPremium = -1.0 Then
PremiumIn = declined
Else
PremiumIn = adjustedPremium
End If
Return PremiumIn
End Function
Any help would be appreciated, thank you
It's pretty straightforward ..
Your sub/function that displays the result should be defined as something like
Private Sub DisplayStuff(premiumInResult as decimal)
'display the value premiumInResult in here
End Sub
You can then either do this which is clearer
Dim result As Decimal = ProcessClaims(yourClaimsList , yourPremiumIn)
DisplayStuff(result)
or
Displaystuff(ProcessClaims(yourClaimsList , yourPremiumIn))
which is shorter but less clear, and makes debugging more difficult

Select Case conversion not displaying properly, how can i fix?

I have an executable that when run takes an inputted date and then selects what date suffix is needed with it, however it is always defaulting to the case else.
when i have 02/04/2017 as format DD/MM/YYYY the result of the below case is 2th April instead of 2nd...
Could anyone enlighten me on the problem.
Dim Datewc As Date = Nothing
If CheckBox1.Checked Then
Datewc = TextBox1.Text
End If
'Determine date suffix
Dim datsuff As String = ""
Select Case CInt(Datewc.Day)
Case 1 Or 21 Or 31
datsuff = "st"
Case 2 Or 22
datsuff = "nd"
Case 3 Or 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Parse you date
Dim dDate As DateTime =
DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Change your code a little, should work.
Day function returns integer, no need to Cast using CInt.
'Determine date suffix
Dim datsuff As String = ""
Select Case Datewc.Day
Case 1 , 21 , 31
datsuff = "st"
Case 2 , 22
datsuff = "nd"
Case 3 , 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Explanation
Case 2 Or 22 means Case ((2 Or 22) ==true), It will be false hence going to else part.

Comparing two cells using Select Case or If Then VBA

I am trying to get different results in cell "C8" depending on the relationship between the values of the cells "B3" and "C3"
I first tried with a basic Select Case :
Sub Salmonpool_depth1()
Dim score As Variant, result As String
With Sheets("Vertical")
score = Range("C3").Value
Select Case score
Case Is = ""
result = ""
Case Is >= 0.3 * Range("B3").Value
result = "0.3"
Case Is >= 0.6 * Range("B3").Value
result = "0.6"
Case Is >= Range("B3").Value
result = "1"
Case Else
result = "0"
End Select
Range("C8").Value = result
End With
End Sub
But it always gave me the result 0.3 as a result if any conditions were filled other than the last one, it did give me 0 when the last condition was filled.
I then tried by defining B3 and C3 as variables
Sub Salmonpool_depth2()
Dim pool As Variant, result As String, hydraulic As Variant
With Sheets("Vertical")
pool = Range("C3").Value
hydraulic = Range("B3").Value
Select Case pool
Case Is = ""
result = ""
Case Is >= 0.3 * hydraulic
result = "0.3"
Case Is >= 0.6 * hydraulic
result = "0.6"
Case Is >= hydraulic
result = "1"
Case Else
result = "0"
End Select
Range("C8").Value = result
End With
End Sub
But that also gave me 0.3 or 0 as a result as above
I then tried with If Then statements instead :
Sub Salmonpool_depth3()
Dim hydraulic As Variant, pool As Variant
hydraulic = Range("B3").Value
pool = Range("C3").Value
If pool >= hydraulic Then
Range("C8").Value = 1
End If
If pool >= 0.6 * hydraulic Then
Range("C8").Value = 0.6
End If
If pool >= 0.3 * hydraulic Then
Range("C8").Value = 0.3
End If
If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
End If
If pool = "" Then
Range("C8").Value = ""
End If
End Sub
But that also gives me 0.3 or 0 as above.
Does anyone have any idea how to change this? It must be in the way that I ask the question as the program does not understand.
Lilou
the problem is that if its greater than .03 then it will always stop there. SELECT CASE Statements exit when one of the conditions evaluates to try. You would need to order it in such a fashion:
Case Is >= hydraulic
result = "1"
Case Is >= 0.6 * hydraulic
result = "0.6"
Case Is >= 0.3 * Range("B3").Value
result = "0.3"
Your problem in the Select Case statements is that once a condition is met, it quits comparing. So when your condition is...say 0.8, the following code says "Is it greater than or equal to 0.3 * Hydraulic? Yes" and then never compares the 0.6 * Hydraulic.
Case Is >= 0.3 * hydraulic
result = "0.3"
Case Is >= 0.6 * hydraulic
You need to put a limit on the first comparison like this:
Case (0.3 * hydraulic) To (0.6 * hydraulic)
result = "0.3"
Case (0.6 * hydraulic) To 1
Using the first bit of code you posted, I think your >= is backwards. It should be <=.
Sub Salmonpool_depth1()
Dim score As Variant, result As String
With Sheets("Vertical")
score = Range("C3").Value
Select Case score
Case Is = ""
result = ""
Case Is <= (0.3 * Range("B3").Value)
result = "0.3"
Case Is >= (0.6 * Range("B3").Value)
result = "0.6"
Case Is >= Range("B3").Value
result = "1"
Case Else
result = "0"
End Select
Range("C8").Value = result
End With
End Sub
I know that Tim, Doug and JC have all answered your question for cases, but as you said your if statements were also giving improper results, and I'm not sure anyone addressed that. As written, changing the order would fix the result, but the more elegant solution is using the Else If statement. That way, you get a situation similar to using Case, where it checks each statement until one is right, does the section of code in that section, and skips ahead to End If when its done. It would look something like this:
If pool >= hydraulic Then
Range("C8").Value = 1
Else If pool >= 0.6 * hydraulic Then
Range("C8").Value = 0.6
Else If pool >= 0.3 * hydraulic Then
Range("C8").Value = 0.3
Else If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
Else If pool = "" Then
Range("C8").Value = ""
End If
This way, lets say pool was 0.7*hydraulic, it would throw False for the first If statement, then True for the next Else If line, set C8 to 0.6, and move to the End If line. Your way (putting End If after each If statement) checks each If statement sequentially, whether something passes the previous statement or not. You can also make this more robust by requiring multiple conditionals to pass each if:
If pool >= hydraulic Then
Range("C8").Value = 1
Else If pool >= 0.6 * hydraulic And pool < hydraulic Then
Range("C8").Value = 0.6
Else If pool >= 0.3 * hydraulic And pool < 0.6 * hydraulic Then
Range("C8").Value = 0.3
Else If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
Else If pool = "" Then
Range("C8").Value = ""
End If
With this snipet, you can reorder the conditionals however you like, and it will only pass if it falls within that specific range. And, if you like, you can place an Else line by itself at the end of the section that will be run if none of the If or Else If statements pass:
If pool >= hydraulic Then
Range("C8").Value = 1
Else If pool >= 0.6 * hydraulic And pool < hydraulic Then
Range("C8").Value = 0.6
Else If pool >= 0.3 * hydraulic And pool < 0.6 * hydraulic Then
Range("C8").Value = 0.3
Else If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
Else If pool = "" Then
Range("C8").Value = ""
Else
Range("C8").Value = "Invalid Entry"
End If

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