VBA Brush Up 05:Decision Making with VBA

Technorati 标签: VBA, if then, select case

来自:Julitta Korol,“Access.2003.Programming.by.Example.with.VBA.XML.and.ASP”,by Wordware Publishing, Inc. 2005, p76-p90

(1)if then结构:单行的不需要end if,多行的需要;单行的如果需要执行多个语句,则加冒号

  1. Sub SimpleIfThen2()
  2.     Dim weeks As String
  3.     On Error GoTo VeryEnd 
  4.     weeks = InputBox("How many weeks are in a year:""Quiz"
  5.     If weeks <> 52 Then MsgBox "Try Again":SimpleIfThen2 
  6.     If weeks = 52 Then MsgBox "Congratulations!"
  7. VeryEnd: 
  8. End Sub 

If condition Then statement1 Else statement2

(2)elseif以及Nested If…Then Statements的用法

  1. Private Sub cmdOK_Click()
  2.     If txtPwd = "FOX" Then 
  3.         MsgBox "You are not authorized to run this report." 
  4.     ElseIf txtPwd = "DOG" Then 
  5.         If txtUser = "John" Then
  6.             MsgBox "You are logged on with restricted privileges." 
  7.         ElseIf txtUser = "Mark" Then 
  8.             MsgBox "Contact the Admin now." 
  9.         ElseIf txtUser = "Anne" Then 
  10.             MsgBox "Go home." 
  11.         Else 
  12.             MsgBox "Incorrect User name."
  13.         End If 
  14.     Else 
  15.         MsgBox "Incorrect password or user name" 
  16.     End If 
  17.     Me.txtUser.SetFocus
  18. End Sub

Here’s the syntax of the If…Then…ElseIf statement:

  1. If condition1 Then 
  2.     statements to be executed if condition1 is True 
  3. ElseIf condition2 Then 
  4.     statements to be executed if condition2 is True 
  5. ElseIf condition3 Then 
  6.     statements to be executed if condition3 is True 
  7. ElseIf conditionN Then 
  8.     statements to be executed if conditionN is True 
  9. Else 
  10.     statements to be executed if all conditions are False 
  11. End If 

The Else clause if optional; you can omit it if there are no actions to be executed when all conditions are false. If the condition is true, Visual Basic will execute the statements between Then and Else, and will ignore the statement between Else and End If. If the condition is false, Visual Basic will omit the statements between Then and Else, and execute the statement between Else and End If.

(3)Select Case Statement:To avoid complex nested If statements that are difficult to follow, you can use the Select Case statement instead. The syntax of this statement is as follows, The Case Else clause is optional. VB执行完match的case之后就立刻退出select.

  1. Select Case testExpression 
  2.     Case expressionList1 
  3.         statements if expressionList1 matches testExpression 
  4.     Case expressionList2 
  5.         statements if expressionList2 matches testExpression 
  6.     Case expressionListN 
  7.         statements if expressionListN matches testExpression 
  8.     Case Else 
  9.         statements to be executed if no values match testExpression 
  10. End Select

(4)在case里用Is做判断、以及Specifying a Range of Values in a Case Clause。once Visual Basic locates a Case clause with a true condition, it doesn’t bother to look at the remaining Case clauses. It jumps over them and continues to execute the procedure with the instructions that may follow the End Select statement.

  1. Select Case unitsSold 
  2.     Case 1 To 100 
  3.         Discount = 0.05 
  4.     Case Is <= 500 
  5.         Discount = 0.1 
  6.     Case 501 To 1000 
  7.         Discount = 0.15 
  8.     Case Is >1000 
  9.         Discount = 0.2 
  10. End Select

(5)Specifying Multiple Expressions in a Case Clause。You may specify multiple conditions within a single Case clause by separating each condition with a comma。The commas used to separate conditions within a Case clause have the same meaning as the OR operator used in the If statement. The Case clause is true if at least one of the conditions is true.

  1. Select Case myMonth 
  2.     Case "January""February""March" 
  3.         Debug.Print myMonth & ": 1st Qtr." 
  4.     Case "April""May""June" 
  5.         Debug.Print myMonth & ": 2nd Qtr." 
  6.     Case "July""August""September" 
  7.         Debug.Print myMonth & ": 3rd Qtr." 
  8.     Case "October""November""December" 
  9.         Debug.Print myMonth & ": 4th Qtr." 
  10. End Select

(6)Here are a few guidelines to help you determine what kind of conditional statement you should use:

    • If you want to supply only one condition, the simple If…Then statement is the best choice.
    • If you need to decide which of two actions to perform, use the If…Then…Else statement.
    • If your procedure requires two or more conditions, use the If…Then…ElseIf or Select Case statements.
    • If your procedure has many conditions, use the Select Case statement. This statement is more flexible and easier to comprehend than the If…Then…ElseIf statement.
【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值