在vba中输入验证和重复

时间:2016-02-10 19:30:38

标签: excel vba validation excel-vba

我有以下代码,我想在4个方面提供帮助:

  
      
  1. 我是否正确验证了输入框?它应该只采取正数值变量。
  2.   
  3. 如何让输入框接受带有和不带符号的输入,如($)?
  4.   
  5. 我如何链接代码,以便在用户输入负数非数字时可以直接请求其他号码?
  6.   
  7. 如何直接在循环中重复该过程而不重复整个代码?
  8.   
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

1 个答案:

答案 0 :(得分:0)

这应该在不深入研究所有代码的情况下回答您的问题。测试

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