Reputation: 1
I have the following code and I would like help in 4 areas:
- Did I validate the inputbox correctly? It should only take positive numerical variables.
- How can I let the input box accept both input with, and without, symbols such as ($)?
- How can I link the code so that it can directly request another number if the user has entered a negative non numeric number?
- How can I repeat the procedure in the loop directly without repeating the whole code?
Option Explicit
Sub IncomeSalaryCalculation()
Dim strSalary As String
Dim dblTaxableSalary As Double
Dim dblTax As Double
Dim dblSalaryAfterTax As Double
Dim strOutput As String
Dim SalaryCalculationRequest As VbMsgBoxResult
strSalary = InputBox("Please indicate your salary", "Salary Calculation")
If Not IsNumeric(strSalary) Then
MsgBox "This is no number! Please enter a non-negatif number", vbOKOnly, "Error"
ElseIf strSalary < 0 Then
MsgBox "You should enter a positive number", vbOKOnly, "Error"
Else
dblTaxableSalary = CDbl(strSalary)
Select Case dblTaxableSalary
Case Is >= 151000
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (150000 - 37400) * 0.5 + (dblTaxableSalary - 150000) * 0.5
Case Is >= 37401
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (dblTaxableSalary - 37400) * 0.4
Case Is >= 2441
dblTax = 2440 * 0.1 + (dblTaxableSalary - 2440) * 0.2
Case Else
dblTax = 2440 * 0.1
End Select
dblSalaryAfterTax = dblTaxableSalary - dblTax
strOutput = "The amount of income tax is " & dblTax & vbNewLine & "Your salary after tax is " & dblSalaryAfterTax
MsgBox strOutput, vbOKOnly, "Final Salary"
Do
SalaryCalculationRequest = MsgBox("Do you want to calculate the tax of a new income?", vbYesNo, "New income tax salary calculation")
If SalaryCalculationRequest = vbYes Then
strSalary = InputBox("Please indicate your salary", "Salary Calculation")
dblTaxableSalary = CDbl(strSalary)
Select Case dblTaxableSalary
Case Is >= 151000
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (150000 - 37400) * 0.5 + (dblTaxableSalary - 150000) * 0.5
Case Is >= 37401
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (dblTaxableSalary - 37400) * 0.4
Case Is >= 2441
dblTax = 2440 * 0.1 + (dblTaxableSalary - 2440) * 0.2
Case Else
dblTax = 2440 * 0.1
End Select
dblSalaryAfterTax = dblTaxableSalary - dblTax
strOutput = "The amount of income tax is " & dblTax & vbNewLine & "Your salary after tax is " & dblSalaryAfterTax
MsgBox strOutput, vbOKOnly, "Final Salary"
Else
MsgBox "Glad to serve you"
End If
Loop Until SalaryCalculationRequest = vbNo
End If
End Sub
Upvotes: 0
Views: 144
Reputation: 788
This should answer your questions without delving into all your code. Tested.
Sub getInput()
Dim ans As String
Do
ans = InputBox("Please indicate your salary", "Salary Calculation")
Loop Until isValid(ans)
''{{{{{{{{
''Do what you need here with the user input (stored in *ans* variable)
''}}}}}}}}
End Sub
Function isValid(ans As String) As Boolean
Dim cleanAns As variant
cleanAns = IIf(ans Like "*$*", Replace(ans, "$", ""), ans)
isValid = IsNumeric(cleanAns) And cleanAns > 0
If Not isValid Then MsgBox "only positive numbers allowed"
End Function
Upvotes: 0