VBA Select Case две переменные - PullRequest
0 голосов
/ 25 октября 2018

Я пытаюсь использовать выбранный случай, чтобы определить повышение заработной платы на основе кода сотрудника и количества занятых лет.Мне трудно использовать регистр выбора с двумя переменными.Ниже мой код:

'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 лет, получают повышение на 4,5%.Кажется, у меня есть Код 3 и сотрудники, которые работали в компании в течение пяти лет для правильной работы, однако я застрял в том, как кодировать «все остальные коды работы» и исключать из нее Код работы 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 голосов
/ 25 октября 2018

Вы можете попробовать этот код, вам не нужно использовать оператор 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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...