📜  如何在 Excel VBA 中使用 Select Case 语句?

📅  最后修改于: 2021-09-23 05:12:14             🧑  作者: Mango

Excel 中的 VBA 代表Visual Basic for Applications ,它是 Microsoft 的编程语言。为了优化性能并减少在 Excel 中的时间,我们需要宏,而 VBA 是后端使用的工具。

一些有用的链接,以获取有关 Excel 中宏、VBA 的更多见解:

  1. 在 Excel 中记录宏。
  2. 如何在Excel中创建宏?

在本文中,我们将讨论如何在 Excel VBA 中使用 Select Case 语句。

执行 :

在 Microsoft Excel 选项卡中,选择Developer Tab 。最初,开发人员选项卡可能不可用。

开发人员选项卡可以通过两步过程轻松启用:

  • 右键单击 Excel 窗口顶部的任何现有选项卡。
  • 现在从弹出菜单中选择自定义功能区。

  • 在 Excel 选项框中,选中开发人员框以启用它,然后单击确定。

  • 现在,开发人员选项卡可见。

  • 现在单击 Developer 选项卡中的Visual Basic选项并创建一个新模块以使用 Select Case 语句编写程序。
Developer  -> Visual Basic -> Tools -> Macros
  • 现在创建一个宏并给出任何合适的名称。

  • 这将打开编辑器窗口,可以在其中编写代码。

选择案例陈述:

select case 语句类似于SWITCH-CASE C、C++、 Java等编程语言中的语句。 Excel 中 Select Case 的结构是:

Select Case Expression/Condition
    Case Val_1
    Block of statements when Expression matches Val_1
    Case Val_2
    Block of statements when Expression matches Val_2
    Case Val_3
    Block of statements when Expression matches Val_3
    .
    .
    .
    Case Else
    Block of code when none of the above conditions match
End Select

Val_1, Val_2,... are the values.

Excel 中 Select Case 中使用的一些重要关键字如下:

  • Case Is : 基本上是和数字一起使用的。
  • Case Else :如果 Cases 的都不与表达式匹配。它类似于 C/C++ 中 SWITCH 语句中的默认值
  • InputBox : 接受用户的输入。
  • MsgBox :向用户显示输出。

示例 1:

我们希望根据学生在考试中获得的分数来显示学生的成绩。考虑如下所示的数据集:

代码 :

Sub Select_Case_Grade()
'Declaring variables to fetch marks and store the grade
Dim marks As Integer, Grade As String
'Fetching marks from the Excel cell
marks = Range("A2").Value
Select Case marks
    Case Is >= 90
        Grade = "S"
    Case Is >= 80
        Grade = "A"
    Case Is >= 70
        Grade = "B"
    Case Is >= 60
        Grade = "C"
    Case Is >= 50
        Grade = "D"
    Case Is >= 40
        result = "E"
    Case Else
        Grade = "F"
End Select
'Displaying the grade in the Excel cell
Range("B2").Value = Grade
End Sub

现在,更改显示等级的标记为“S”。

您还可以使用数字范围而不是Case Is来编写前面的代码。

Sub Select_Case_Grade()
'Declaring variables to fetch marks and store the grade
Dim marks As Integer, Grade As String
'Fetching marks from the Excel cell
marks = Range("A2").Value
Select Case marks
    Case 91 To 100
        Grade = "S"
    Case 81 To 90
        Grade = "A"
    Case 71 To 80
        Grade = "B"
    Case 61 To 70
        Grade = "C"
    Case 51 To 60
        Grade = "D"
    Case 40 To 50
        result = "E"
    Case Else
        Grade = "F"
End Select
'Displaying the grade in the Excel cell
Range("B2").Value = Grade
End Sub

示例 2:考虑在公司中,员工必须轮班处理某个项目。该公司希望根据奇偶规则分配班次,并将年龄作为决定标准。如果员工的年龄是奇数,那么他/她必须在夜班工作,如果是偶数,则在早班工作。

选择用户可以在框中输入数据的案例。

Sub Select_Case_Allocate()
'Declaring variables to fetch marks and store the grade
Dim Age As Integer
'Asking the user to enter the age
Age = InputBox("Enter Your Age:")
Select Case (Age Mod 2) = 0
    Case True
    MsgBox "You will work in the morning shift"
    Case False
    MsgBox "You will work in the night shift"
End Select
End Sub

示例 3:让我们创建一个小型计算器,它将两个数字作为输入并执行这些数字的加法和乘法。

代码 :

Sub Select_Case_Calculator()
'Declaring variables to fetch marks and store the grade
Dim num1 As Integer, mum2 As Integer, operator As String, res As Integer
'Asking the user to enter the numbers and operator to calculate
num1 = InputBox("Enter The First Number:")
num2 = InputBox("Enter The Second Number:")
operator = InputBox("Enter The Operator Name(Sum,Mul):")
Select Case operator
    Case "Sum"
    res = num1 + num2
    MsgBox ("The result is :" & res)
    Case "Mul"
    res = num1 * num2
    MsgBox ("The result is :" & res)
    Case Else
    MsgBox "Please Enter a Valid Operator"
End Select
End Sub

我们可以修改上面的代码,在case中使用多个条件。例如,用户可以输入字符串Sum 为“SUM”或“sum”,因为 Excel 对话框是区分大小写的。

Sub Select_Case_Calculator()
'Declaring variables to fetch marks and store the grade
Dim num1 As Integer, mum2 As Integer, operator As String, res As Integer
'Asking the user to enter the numbers
num1 = InputBox("Enter The First Number:")
num2 = InputBox("Enter The Second Number:")
operator = InputBox("Enter The Operator Name(Sum,Mul):")
Select Case operator
    Case "Sum", "SUM", "sum", "SUm", "SuM", "suM", "sUm"
    res = num1 + num2
    MsgBox ("The result is :" & res)
    Case "Mul", "mul", "MUL", "MuL", "muL", "mUl", "MUl"
    res = num1 * num2
    MsgBox ("The result is :" & res)
    Case Else
    MsgBox "Please Enter a Valid Operator"
End Select
End Sub

示例 4:让我们看一个使用嵌套 Select Case 的示例。

考虑一家公司,该公司有一个关于员工一年可以休假总数的政策部门。现在有多个部门,有女性也有男性员工,每个人都有不同的请假政策。因此,使用嵌套的 Select Case 来构建问题陈述,用户可以在其中输入部门和性别的详细信息,以检查他们一年中最多可以休假的天数。

Sub Select_Case_Empleave()
'Declaring variables to fetch Department and gender of employee
Dim Department As String, sex As String
'Asking the user to enter the details
Department = InputBox("Enter Your Department:")
sex = InputBox("Enter Your Gender (Male,Female):")
Select Case Department
    Case "HR"
    Select Case sex
        Case "Male"
        MsgBox ("You can take maximum 10 days leave in an year")
        Case "Female"
        MsgBox ("You can take maximum 20 days leave in an year")
        Case Else
        MsgBox ("Invalid Gender")
    End Select
    Case "IT"
    Select Case sex
        Case "Male"
        MsgBox ("You can take maximum 15 days leave in an year")
        Case "Female"
        MsgBox ("You can take maximum 25 days leave in an year")
        Case Else
        MsgBox ("Invalid Gender")
    End Select
Case Else