码迷,mamicode.com
首页 > 编程语言 > 详细

VBA Promming——分支语句(解二元一次方程)

时间:2018-10-04 17:26:40      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:width   lan   info   tar   get   cell   sed   技术分享   use   

分支语句

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

 

VBA Promming——分支语句(解二元一次方程)

标签:width   lan   info   tar   get   cell   sed   技术分享   use   

原文地址:https://www.cnblogs.com/lfri/p/9742550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!