Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1591|回复: 0

[技巧] Select-Case结构

[复制链接]
发表于 2014-5-20 12:55 | 显示全部楼层 |阅读模式
        在Excel VBA中,Select-Case主要用于处理三个或者多个选项之间选择的选项。当程序的选项比较多的时候,Select-Case结构是比IF-Then-Else结构更好的选择。首先,从程序结构角度来看,Select-Case结构显然比If-Then-Else结构更加易读。
        在VBA中,Select-Case结构的语法结构如下:
        Select Case testexpression
            Case expression - 1
                instructions - 1
            Case expression - 2
                instructions - 2
            ..............................
            Case expression - n
                instructions - n
        End Select
        在Select-Case语法结构中,灵活使用条件逻辑表达式,要吧处理多种复杂的问题。其中, 逻辑表达式可以是具体的数值,也可以是数据范围,或者是比较运算符等。 下面使用一个简单的盒子来说明Select-Case结构的使用方法。
        例如,使用Select-Case结构来获取不同的折扣信息,具体折扣信息如下:
        a. 销售数量在5000以上,折扣是0.65;
        b. 销售数量在1500~5000,折扣是0.85;
        c. 销售数量在800~1500,折扣是0.95;
        d. 销售数量在800以下,全额支付。
        根据上面的折扣信息,编写对应的程序代码如下:
        步骤1、进入VBE,打开代码窗口,在其中输入代码如下:
            Sub Check_Quantity_Select ()
            Dim intSaleQuantity As Integer
            Dim strMsg As String
            Dim strMsgRev As String
            intSaleQuantity = CInt(InputBox("Please Enter the quantity"))
            strMsg = "折扣是"
            Select Case intSaleQuantity
            Case Is >= 5000
                 strMsgRev = strMsg & "0.65"
                 MsgBox strMsgRev
            Case 1500 To 5000            
                 strMsgRev = strMsg & "0.85"
                 MsgBox strMsgRev
            Case 800 To 1500            
                 strMsgRev = strMsg & "0.95"
                 MsgBox strMsgRev   
            Case Else
                 MsgBox "请付全额"
            End Select
            End Sub
        步骤2、在VBE中,按F5键测试程序。在输入框中输入5500,则对话框提示“折扣是0.65”。
        说明:在以上的代码中使用了Cint函数,将函数Inputbox接收的字符串变量转换为整型变量。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-3 20:01 , Processed in 0.212178 second(s), 6 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表