VBA Select Case两个变量 [英] VBA Select Case two variables

查看:535
本文介绍了VBA Select Case两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use select case to determine a pay raise based on employee code and number of years employed. 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

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

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

推荐答案

您可以尝试使用此代码,而不必使用case语句.

You can try this code, you doesn't have to use the case statement.

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))

这篇关于VBA Select Case两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆