简体   繁体   English

VBA Select Case 两个变量

[英]VBA Select Case two variables

I am trying to use select case to determine a pay raise based on employee code and number of years employed.我正在尝试使用 select case 根据员工代码和受雇年数确定加薪。 I am having a hard time using select case with two variables.我很难使用带有两个变量的选择案例。 Below is my code:下面是我的代码:

'declare variables
Dim strCode As String
Dim strYears As String
Dim intYears As Integer
Dim sngRaise As Single

'enter employee code and years employed
strCode = InputBox(prompt:="Enter the employee code: ", Title:="Code")
strYears = InputBox(prompt:="Enter the number of years employed: ", _
   Title:="Years")

'convert years to a number and convert code to uppercase
intYears = Val(strYears)
strCode = UCase(strCode)

'assign raise rate
Select Case True
Case Is = "A" And strYears >= 3
    sngRaise = 0.04
Case "B" And strYears >= 5
    sngRaise = 0.05
Case Else
    sngRaise = 0.03
End Select

MsgBox prompt:=Format(expression:=sngRaise, Format:="percent"), _
Buttons:=vbOKOnly + vbInformation, Title:="Raise Rate"


End Sub

I am trying to make it that all Code 3 employees and all employees who have been with the company for five years are to receive a 5% raise and employees having other job codes or those who have been with the company for less than 5 years get a 4.5% raise.我正在努力使所有 Code 3 员工和所有在公司工作五年的员工都获得 5% 的加薪,而拥有其他工作代码的员工或在公司工作不到 5 年的员工将获得加薪上涨 4.5%。 I seem to have got Code 3 and employees who have been with the company for five years to run correctly, however, I am stuck on how to code "all other job codes" and exclude job Code 3 from it.我似乎让代码 3 和在公司工作了五年的员工正确运行,但是,我被困在如何编码“所有其他工作代码”并从中排除工作代码 3。 Below is my code下面是我的代码

'declare variables
 Dim intCode As Integer
 Dim strYears As String
 Dim intYears As Integer
 Dim sngRaise As Single

 'enter employee code and years employed
 intCode = InputBox(prompt:="Enter the employee 
 code: ", Title:="Code")
 strYears = InputBox(prompt:="Enter the number of 
  years employed: ", _
 Title:="Years")

'convert years to a number
 intYears = Val(strYears)

'assign raise rate
 Select Case True
 Case intCode = "3" Or strYears >= 5
sngRaise = 0.05
 Case intCode = 1 To 2 Or strYears <= 5
sngRaise = 0.045

End Select

MsgBox prompt:=Format(expression:=sngRaise, 
Format:="percent"), _
Buttons:=vbOKOnly + vbInformation, Title:="Raise 
Rate"

You can try this code, you doesn't have to use the case statement.你可以试试这个代码,你不必使用case语句。

Dim raise(3, 2)  'job code, year less then 5/greater than 5

raise(1, 1) = 0.045
raise(1, 2) = 0.05
raise(2, 1) = 0.045
raise(2, 2) = 0.05
raise(3, 1) = 0.05
raise(3, 2) = 0.05


intCode = InputBox(prompt:="Enter the employee  code: ", Title:="Code")
strYears = InputBox(prompt:="Enter the number of years employed: ", Title:="Years")

If (intYears >= 5) Then
    intYears = 2
Else
    intYears = 1
End If

MsgBox ("The percentage of raise is: " & raise(intCode, intYears))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM