Syntax error while executing if and select statement in vba - vba

I am learning basic VBA. When executing the below program in Excel 2013, I am getting a syntax error every time.
Sub ShowDiscount3()
Dim Quantity As Integer
Dim Discount As Double
Quantity = InputBox(“Enter the quantity “)
Select Case Quantity
Case 0 To 24
Discount = 0.1
Case 25 To 49
Discount = 0.15
Case 50 To 74
Discount = 0.2
Case Is >= 75
Discount = 0.25
End Select
MsgBox “Discount: “ & Discount
End Sub

Do not use: “
Use " instead:
Sub ShowDiscount3()
Dim Quantity As Integer
Dim Discount As Double
Quantity = InputBox("Enter the quantity")
Select Case Quantity
Case 0 To 24
Discount = 0.1
Case 25 To 49
Discount = 0.15
Case 50 To 74
Discount = 0.2
Case Is >= 75
Discount = 0.25
End Select
MsgBox "Discount: " & Discount
End Sub

Related

How to find federal taxes based on salary ranges

I'm designing a software that does basic payroll for a school project. I'm stuck on this one part where I have to figure out what the federal tax will be based on the employee salary. This is the chart that tells you the rate of the tax based on salary range.
I tried this code,
Dim federaltaxrate As Integer
Dim federaltax = (salary.Text * federaltaxrate)
If salary.Text >= 0 Then
If salary.Text <= 50 Then
federaltaxrate = 0
End If
ElseIf salary.text <= 500 Then
If salary.Text >= 50 Then
federaltaxrate = 0.1
End If
ElseIf salary.text <= 2500 Then
If salary.Text >= 500 Then
federaltaxrate = 45 + 0.15 * salary.Text - 500
End If
ElseIf salary.text <= 5000 Then
If salary.Text >= 2500 Then
federaltaxrate = 345 + 0.2 * salary.Text - 2500
End If
ElseIf salary.text >= 5000 Then
federaltaxrate = 845 + 0.25 * salary.Text - 5000
End If
Else
End If
I have a listbox that shows other information as well but this is what I used to show the calculated info in the listbox.
ListBox1.Items.Add("Federal Tax: $" + federaltax.ToString)
When I run this code and input in a random salary, the federal tax shows up as 0.
Do I need to convert the salary into weekly gross pay, if so how would I go on about writing the code that finds the federal tax rate based on the salary and it's range.
You might be having trouble with order of precedence of the arithmetic operations. I think a Select Case is cleaner.
Private Function GetFederalTax(GrossPay As Decimal) As Decimal
Dim FederalTax As Decimal
Select Case GrossPay
Case < 50
FederalTax = 0
Case < 500
FederalTax = CDec((GrossPay - 51) * 0.1)
Case < 2500
FederalTax = CDec(((GrossPay - 500) * 0.15) + 45)
Case < 5000
FederalTax = CDec(((GrossPay - 2500) * 0.2) + 345)
Case Else
FederalTax = CDec(((GrossPay - 5000) * 0.25) + 845)
End Select
Return FederalTax
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim grossPay As Decimal
If Decimal.TryParse(TextBox1.Text, grossPay) Then
Dim tax = GetFederalTax(grossPay)
Debug.Print($"Gross {grossPay} Tax {tax}")
ListBox1.Items.Add(String.Format("Federal Tax {0}, Gross Pay {1}", tax, grossPay)) '***EDIT***
Else
MessageBox.Show("Please enter a valid number.")
End If
End Sub
The sample test produced the following in the Immediate Window.
Gross 45 Tax 0
Gross 700 Tax 75
Gross 8000 Tax 1595
Gross 2501 Tax 345.2
Gross 2800 Tax 405
Firstly, your Boolean logic is all wrong. If the salary value was 51, you'd satisfy the first outer condition (>= 0) and then fail the first inner condition (<= 50). There would not be any further comparisons performed - which you would know if you'd debugged - so no tax calculation would ever be performed.
Secondly, your calculations are OK but you're misusing the results. Those calculation get the amount of tax to be paid, not the rate. The rates are 10%, 15%, 20% and 25%, which are already contained in your calculations. Get rid of that second variable and just assign the results of appropriate calculations to the one variable.
I would do it like this:
Dim salaryAmount = CDec(salary.Text)
Dim taxAmount As Decimal = Decimal.Zero
If salaryAmount > 5000D Then
taxAmount = 845D + 0.25D * (salaryAmount - 5000D)
ElseIf salaryAmount > 2500D Then
taxAmount = 345D + 0.2D * (salaryAmount - 2500D)
ElseIf salaryAmount > 500D Then
taxAmount = 45D + 0.15D * (salaryAmount - 500D)
ElseIf salaryAmount > 50D Then
taxAmount = 0.1D * (salaryAmount - 50D)
End If
'Use taxAmount here.
This uses appropriate data types throughout, i.e. it does not perform arithmetic on String values and it uses Decimal for currency values. The D suffix on the literals forces them to be type Decimal rather than Integer or Double.
It also works from biggest to smallest to simplify the Boolean expressions.
The Nested If should be combined like below as it is missing few cases
If salary.Text >= 0 And salary.Text <= 50 Then
federaltaxrate = 0
ElseIf salary.text <= 500 And salary.Text >= 50 Then
federaltaxrate = 0.1
ElseIf salary.text <= 2500 AND salary.Text >= 500 Then
federaltaxrate = 45 + 0.15 * salary.Text - 500
End If

Function does not work - shows 0

I have written a simple function that counts a company bonus. For some reason, it shows 0 and does not give an answer.
Function insurancebonus(penetration As Single, penetrationPlan As Single, _
renewalRate As Single, renewalPlan As Single, dealerRank As Single, _
premiumDynamic As Single) As Single
Dim Total As Single
Total = 0
If penetration >= penetrationPlan Then Total = Total + 0.01
If renewalRate >= renewalPlan Then Total = Total + 0.01
Select Case dealerRank
Case 1
Select Case premiumDynamic
Case 0 To 0.03: Total = Total + 0.01
Case 0.03 To 0.05: Total = Total + 0.02
Case 0.05 To 1: Total = Total + 0.03
End Select
Case 2
Select Case premiumDynamic
Case 0.02 To 0.04: Total = Total + 0.01
Case 0.04 To 0.08: Total = Total + 0.02
Case 0.08 To 1: Total = Total + 0.03
End Select
Case 3
Select Case premiumDynamic
Case 0.05 To 0.1: Total = Total + 0.01
Case 0.1 To 0.15: Total = Total + 0.02
Case 0.15 To 1: Total = Total + 0.05
End Select
Case 4
Select Case premiumDynamic
Case 0.1 To 0.17: Total = Total + 0.01
Case 0.17 To 0.25: Total = Total + 0.02
Case 0.25 To 1: Total = Total + 0.03
End Select
Case 5
Select Case premiumDynamic
Case 0.15 To 0.3: Total = Total + 0.01
Case 0.3 To 0.4: Total = Total + 0.02
Case 0.4 To 1: Total = Total + 0.03
End Select
End Select
End Function
You haven't told the function to return anything.
I assume you want to return the value of Total?
If that is the case, prior to the End Function add the following line:
insurancebonus = Total
This specifies that your function should return the value of Total.
As a very short and contrived example, this function simply returns 1.
Public Function test() As Integer
test = 1
End Function
The point is that you must assign the value to the name of your function.
For completeness, if your function returns an Object type you must use the Set keyword, such as:
Public Function testRange() As Range
Set testRange = Range("A1")
End Function
Which would return a Range object.

Can't understand why my variable doesn't change

I'm building a little time > pay conversion program in VB. I'm really new to VB and don't understand why my variable pay doesn't calculate like it should. I plug in 5 5's as a test and get $0.
Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0
While timeList.Items.Count < 5
time = timeList.Items(timeCounter)
total += time
timeCounter += 1
End While
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
Else
PayLabel.Text = "Error"
End If
End If
PayLabel.Text = "$" & pay
Your syntax should be something like this:
For intCount = 0 To timeList.Items.Count
time = timeList.Items(intCount)
total += time
Next intCount
This will avoid an infinite loop.
To fix your 40+ issue:
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
End If
Else
PayLabel.Text = "Error"
End If
this would be my fix into a console apps
for process will return $0, second $100
Module Module1
Sub Main()
Dim timeList As New List(Of Integer)
timeList.AddRange(New Integer() {1, 2, 3, 4, 5, 6})
process(timeList)
timeList.Clear()
timeList.AddRange(New Integer() {1, 2, 3, 4})
process(timeList)
Console.Read()
End Sub
Private Sub process(timeList As List(Of Integer))
Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0
While timeList.Count < 5 AndAlso timeCounter < timeList.Count
time = timeList(timeCounter)
total += time
timeCounter += 1
End While
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
Else
Console.WriteLine("error")
End If
End If
Console.WriteLine("$" & pay)
End Sub
End Module
This could be better solved with a functional approach. To get the sum of the list of integers do the following:
Dim totalTime = timeList.Sum()
Then you can follow the logic you laid out. I would highly recommend learning to use Linq Set Functions to make your code your readable and easier to understand. Good Luck.

Trying to get vba to "loop until" with a count

I'm trying to get the code to read a value from an Input box which is the desired investment amount ie. 3000. Then to read down a list (40 rows long) of the amount of btc available at a particular price, and consecutively sum up these total dollar amounts (quantity*price) going down the list until the point where adding the next line would be greater than the desired investment amount (ie. I'm trying to see the cheapest way to acquire a bunch of btc).
I'll then write a bit to make it make up the rest of the value from the next line as it obviously won't be reached perfectly but I can't seem to get this bit to work. When I execute the code I'm getting some weird results that don't make much sense. I've put an example of a table in so you can see what I'm working with (The first price 94.25 is B3/ActiveCell)
This is probably extremely trivial but I've never done any of this stuff before. Thanks for your time and I hope I've outlined it clearly enough.
Sub Projected()
Dim InvestValue As Single
Dim SumBTCE As Single
Dim Sumup As Single
Dim NumBTC As Single
Dim Count As Integer
InvestValue = InputBox("Input investment amount:")
NumBTC = 0
Sumup = 0
ActiveWorkbook.Sheets("BTC-E Data").Cells(3, 2).Select
Do Until (Sumup + (ActiveCell.Offset(Count, 0).Value * ActiveCell.Offset(Count, 1).Value)) >= InvestValue
For Count = 1 To 40
Sumup = Sumup + ActiveCell.Offset(Count - 1, 0).Value * ActiveCell.Offset(Count - 1, 1).Value
NumBTC = NumBTC + ActiveCell.Offset(0, 1).Value
Next Count
Loop
MsgBox NumBTC
MsgBox Sumup
End Sub
price BTC USD
94.25 0.1 9.425
94.439 0.34583324 32.66014535
94.44 2 188.88
94.443 0.011 1.038873
94.444 0.4 37.7776
94.493 0.025 2.362325
94.5 0.1 9.45
94.55 0.1 9.455
94.6 0.1 9.46
94.601 0.5 47.3005
94.648 0.0112 1.0600576
94.649 4.12801098 390.7121112
94.65 35.75926753 3384.614672
94.664 2.128011 201.4460333
94.665 3.5 331.3275
94.679 0.1395 13.2077205
94.68 0.15 14.202
94.689 2.128011 201.4992336
94.69 18.73708352 1774.214439
94.698 0.010978 1.03959464
94.699 0.093 8.807007
94.7 0.1 9.47
94.704 0.025 2.3676
94.736 0.0837 7.9294032
94.737 0.09 8.52633
94.749 2.128011 201.6269142
94.75 20.1 1904.475
94.755 0.1 9.4755
94.8 0.1 9.48
94.801 0.03758691 3.56327665
94.81 5.7236763 542.66175
94.829 0.15 14.22435
94.84 0.20095058 19.058153
94.85 0.1 9.485
94.87 0.01 0.9487
94.879 0.401 38.046479
94.88 0.01 0.9488
94.887 0.40930425 38.83765236
94.89 0.01 0.9489
94.9 0.30106377 28.57095176
Here's how I'd do it:
A2 contains your goal, e.g., $3000.
C2:E41 contains your data
F2 formula:
=SUMPRODUCT((C$2:C2*D$2:D2))
G2 formula:
=SUM(F$2:F2)>=$A$2
H2 formula:
=IF(G2,MAX(0,$A$2-SUM(F$1:F1)),D2)
Then copy the formulas down.
You could combine these formulas, but it's easier to follow this way.
Try the code below
Sub Projected()
Dim InvestValue, SumBTCE, Sumup, NumBTC As Single
Dim Count, LastRow, BTC, Price As Integer
InvestValue = InputBox("Input investment amount:")
NumBTC = 0
Sumup = 0
With Worksheets("BTC-E Data")
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row 'finding the last row in column 2
End With
For Count = 3 To LastRow
Price = ActiveWorkbook.Sheets("BTC-E Data").Range("B" & Count).Value
BTC = ActiveWorkbook.Sheets("BTC-E Data").Range("C" & Count).Value
Sumup = Sumup + (Price * BTC)
NumBTC = NumBTC + BTC
If Sumup >= InvestValue Then Exit For
Next
MsgBox NumBTC
MsgBox Sumup
End Sub
Output of the above code
NumBTC = 100.2461
Sumup = 9520.399976

VB Simple If statements (h/w)

I don't deal with VB. I am helping my mother with her homework and just cant think straight anymore on what to do with this statement.
Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
Dim intPackA As Integer = 0
Dim intPackB As Integer = 0
Dim intPackC As Integer = 0
Dim SavingsPriceB As Decimal = 0.0
Dim SavingsPriceC As Decimal = 0.0
Dim TotalPrice As Decimal = 0.0
lblTotal.Text = String.Empty
If radPackA.Checked Then
TotalPrice = 9.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackA) Then
If intPackA > 10 Then
TotalPrice = TotalPrice + ((intPackA - 10) * 2)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
SavingsPriceB = 14.95 + ((intPackB - 20) * 1)
SavingsPriceC = 19.95
If TotalPrice < SavingsPriceB And TotalPrice < SavingsPriceC Then
lblTotal.Text = TotalPrice & ", no savings with Package B or C"
End If
End If
ElseIf radPackB.Checked Then
TotalPrice = 14.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackB) Then
If intPackB > 20 Then
TotalPrice = TotalPrice + ((intPackB - 20) * 1)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
End If
ElseIf radPackC.Checked Then
TotalPrice = 19.95
lblTotal.Text = TotalPrice
End If
If chkNonprofit.Checked Then
TotalPrice = Format((TotalPrice - ((TotalPrice / 100) * 20)), ".00")
lblTotal.Text = TotalPrice & ControlChars.CrLf & "Non-Profit Organization discount applied"
End If
End Sub
It's the If chkSavings.Checked that's giving me problem.
This is the program as designed. There is a label bellow the packages that displays the total.
When the Potential Savings is checked, it should also display the amount you could save if you use a different package.
So if I put Package A, 5 hours, 20% discount it should say $7.96, no savings with Package B or C. For Package A, 25 hours it should say $39.95, save $20.00 with Package B, and save $20.00 with Package C
The code I have does not print it even the first part.
Package A and less then 10 hours = $9.95, every additional hour is $2.00 more
Package B and less then 20 hours = $14.95, every additional hour is $1.00 more
Package C with unlimited hours is = $19.95
So My question is, what am I doing wrong in my code or how could I achieve what I am looking for.