zoukankan      html  css  js  c++  java
  • VBA Promming——分支语句(解二元一次方程)

    分支语句

    1 If  expression1 Then
    2       expressions
    3 ElseIf expression2 Then
    4        expressions
    5 Else
    6       expression
    7 End If

    注:VBA中等于号和赋值符号都是"=",但并不会冲突,只有在选择语句中“=”才表示是否相等

    示例(交互型)

    1、写好程序

    2、设置动作 (View-->Toolbars-->Form Controls-->Toggle Design Mode-->Push Button)

    3、绑定对应的宏 (Controls-->Events-->Mouse button pressed-->Macro-->想绑定的Macro)

    代码

     1 Option Explicit           ‘设置变量必须先定义才能使用’
     2 Option VBASupport 1       ‘必须写,使得VBA能够在LibreOffice中起作用’
     3 Sub Main
     4     Dim A As Double
     5     Dim B As Double
     6     Dim C As Double
     7     Dim detal As Double
     8     A = ActiveSheet.Cells(4,2)
     9     B = ActiveSheet.Cells(5,2)
    10     C = ActiveSheet.Cells(6,2)
    11     detal = B ^ 2 - 4 * A * C
    12     
    13     Dim x1 As Double
    14     Dim x2 As Double
    15     ActiveSheet.Cells(10,2) = ""
    16     ActiveSheet.Cells(11,2) = ""
    17     If detal > 0 Then
    18         MsgBox("two solution!")
    19         x1 = (-B + sqr(detal)) / (2 * A)
    20         x2 = (-B - sqr(detal)) / (2 * A)
    21         ActiveSheet.Cells(10,2) = x1
    22         ActiveSheet.Cells(11,2) = x2
    23     Elseif detal = 0  Then
    24         MsgBox("one solution!")
    25         x1 = -B / (2 * A)
    26         x2 = x1
    27         ActiveSheet.Cells(10,2) = x1
    28         ActiveSheet.Cells(11,2) = x2
    29     Else 
    30         MsgBox("no solution!")
    31         ActiveSheet.Cells(10,2) = "no solution"
    32         ActiveSheet.Cells(11,2) = "no solution"
    33     End if
    34 End Sub
    35 
    36 Sub clear
    37     ActiveSheet.Cells(4,2) = ""
    38     ActiveSheet.Cells(5,2) = ""
    39     ActiveSheet.Cells(6,2) = ""
    40     ActiveSheet.Cells(10,2) = ""
    41     ActiveSheet.Cells(11,2) = ""
    42 End sub

    效果图

    参考链接:https://youtu.be/TrjUbaqspNg

  • 相关阅读:
    printf,sprintf,fprintf的区别与联系
    linux repo init 遇到的问题
    POSTMAN使用说明
    HTTP协议-深入了解
    HTTP协议-详解篇
    HTTP协议-基础篇
    多线程学习
    I/O流
    String类学习&泛型
    集合
  • 原文地址:https://www.cnblogs.com/lfri/p/9742550.html
Copyright © 2011-2022 走看看