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.
内容概要:本文系统研究了跟网型T型三电平逆变器在新能源并网背景下的多目标协同控制策略,重点围绕低电压穿越(LVRT)、电能质量优化、中点电位平衡及故障穿越能力等关键问题展开。基于Simulink平台构建了完整的仿真系统,深入分析并改进了电流环控制、SVPWM调制策略、序阻抗建模与稳定性分析等核心技术,旨在提升逆变器在不对称电网故障等复杂工况下的运行性能与并网可靠性。研究还融合虚拟同步发电机(VSG)与构网型变流器等先进控制理念,探讨其在弱电网环境中的动态响应与稳定性表现,并通过大量仿真验证各类控制策略的有效性与鲁棒性。; 适合人群:具备电力电子、新能源并网或自动控制等相关专业知识基础,从事新能源发电、电力系统仿真与控制领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 深入掌握T型三电平逆变器在电网故障下的多目标控制难点与协同优化解决方案;② 熟练运用改进电流解耦、SVPWM优化与中点电位平衡等关键技术进行仿真设计与参数整定;③ 为新能源发电系统的控制器开发、并网性能评估及高水平学术论文复现提供可靠的技术支持与模型参考。; 阅读建议:本资源整合了多项前沿仿真研究成果,建议读者结合自身研究方向选择典型案例深入学习,重点关注控制逻辑设计、Simulink模型搭建、参数整定与仿真结果分析过程,并充分利用提供的模型与代码资源进行复现与调试,以深化对理论原理的理解和实际工程应用能力的培养。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值