Comparing two cells using Select Case or If Then VBA - 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

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

VBA Else without if error

I keep getting the " Else without if" error in VBA when I clearly do not have that issue. Does anyone know how to fix it? It takes me to the elseif statement that begins with elseif memb= "platinum"
Below is my code:
ElseIf memb = "Platinum" Then d = 0.01
ElseIf memb = "Black" Then d = 0.03
End If
If st >= min Then cb = st * d Else cb = 0
End If
If cb >= thresh Then MsgBox ("cb is greater than thresh")
End If
tac = st + cb
Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac
I'm going to assume your first If statement goes something like this:
If memb = "Gold" Then d = 0.005
ElseIf memb = "Platinum" Then d = 0.01
ElseIf memb = "Black" Then d = 0.03
End If
If some processing is performed on the same line as the Then keyword, VBA treats it as a single, non-nested If statement. This means that anything after that will be treated as a new statement and not related to prior If statement.
What you can do is put the processing statement(s) on the next line after each If-Then and ElseIf-Then statements.
Example,
If memb = "Gold" Then
d = 0.005
ElseIf memb = "Platinum" Then
d = 0.01
ElseIf memb = "Black" Then
d = 0.03
End If
With this in mind, you may want to fix the succeeding If-Then-Else statements in your code. The End If part becomes meaningless if your If-Then-Else is in a single line.
Your code seems to have syntax error and error message tells you that.
Or you did not post all code?
Have a look on MS documentation: https://msdn.microsoft.com/de-de/library/752y8abs.aspx
Do you really stick to the syntax?
Without even having MS OFfice this should (be better readable and) work:
If memb = "Platinum" Then
d = 0.01
ElseIf memb = "Black" Then
d = 0.03
End If
If st >= min Then
cb = st * d
Else
cb = 0
End If
If cb >= thresh Then
MsgBox ("cb is greater than thresh")
End If
tac = st + cb
Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac

Visual Basic number range

I'm trying to complete an assignment but I'm a little lost in the logic here. I'm trying to create a range for a kWh rate to applied for but can't seem to come up with anything. I'm new to vb and programming.
Dim stateTax As Decimal = 3.5
Dim cityTax As Decimal = 1.5
Dim kWhUsed As Decimal = txtkWhUsed.Text
Dim kWhRate As Decimal
Select Case True
Case (kWhUsed < 1000)
kWhRate = 0.052
RunTotalPrice = (kWhRate * kWhUsed)
Case (kWhUsed >= 1000)
kWhRate = 0.041
RunTotalPrice = RunTotalPrice + (kWhRate * kWhUsed)
End Select
txtAmtDue.Text = FormatCurrency(RunTotalPrice.ToString, 2)
End Sub
Use a Select Case since it does top down logic testing for you.
Dim kWhRate As Double
Select Case kWhUsed
Case < 1000
kWhRate = 0.052
Case < 2000
kWhRate = 0.041
'etc.
End Select

Calculation Issue in VB 2010

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.