VBA Select Case两个变量

时间:2018-10-25 02:48:21

标签: excel vba excel-vba

我正在尝试使用精选案例来根据员工守则和受雇年限确定加薪幅度。我很难使用带有两个变量的选择大小写。下面是我的代码:

'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

我试图使所有Code 3员工和所有在公司工作5年的员工都获得5%的加薪,并且拥有其他工作代码的员工或在公司工作少于10年的员工5年加薪4.5%。我似乎已经有了Code 3,并且已经在公司工作了5年的员工才能正常运行,但是,我仍然坚持如何编写“所有其他工作代码”并从中排除工作Code 3。下面是我的代码

'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"

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码,而不必使用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))