VBA Promming——分支语句(解二元一次方程)
2024-08-30 06:46:26
分支语句
If expression1 Then
expressions
ElseIf expression2 Then
expressions
Else
expression
End If
注:VBA中等于号和赋值符号都是"=",但并不会冲突,只有在选择语句中“=”才表示是否相等
示例(交互型)
1、写好程序
2、设置动作 (View-->Toolbars-->Form Controls-->Toggle Design Mode-->Push Button)
3、绑定对应的宏 (Controls-->Events-->Mouse button pressed-->Macro-->想绑定的Macro)
代码
Option Explicit ‘设置变量必须先定义才能使用’
Option VBASupport 1 ‘必须写,使得VBA能够在LibreOffice中起作用’
Sub Main
Dim A As Double
Dim B As Double
Dim C As Double
Dim detal As Double
A = ActiveSheet.Cells(,)
B = ActiveSheet.Cells(,)
C = ActiveSheet.Cells(,)
detal = B ^ - * A * C Dim x1 As Double
Dim x2 As Double
ActiveSheet.Cells(,) = ""
ActiveSheet.Cells(,) = ""
If detal > Then
MsgBox("two solution!")
x1 = (-B + sqr(detal)) / ( * A)
x2 = (-B - sqr(detal)) / ( * A)
ActiveSheet.Cells(,) = x1
ActiveSheet.Cells(,) = x2
Elseif detal = Then
MsgBox("one solution!")
x1 = -B / ( * A)
x2 = x1
ActiveSheet.Cells(,) = x1
ActiveSheet.Cells(,) = x2
Else
MsgBox("no solution!")
ActiveSheet.Cells(,) = "no solution"
ActiveSheet.Cells(,) = "no solution"
End if
End Sub Sub clear
ActiveSheet.Cells(,) = ""
ActiveSheet.Cells(,) = ""
ActiveSheet.Cells(,) = ""
ActiveSheet.Cells(,) = ""
ActiveSheet.Cells(,) = ""
End sub
效果图
参考链接:https://youtu.be/TrjUbaqspNg
最新文章
- Silverlight5 Tools安装失败及解决方案
- 【mongo】drop不释放磁盘空间
- mysql的登录密码带特殊符号登录不进去的问题
- careercup-数学与概率 7.5
- 商务通代码API
- Auto-Layout 的各种坑Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil'
- ajax是怎么发请求的和浏览器发的请求一样吗?cookie
- POJ 2773 Happy 2006 数学题
- python学习第五讲,python基础语法之函数语法,与Import导入模块.
- vue 3.0的搭建
- docker 命令2
- Ubuntu下Tomcat绑定80端口(zz)
- javascript的use strict(使用严格模式)
- Huawei DHCP 全局配置与接口配置
- 深入set和dict
- linux下mysql自动备份脚本
- PL/SQL集合(一):记录类型(TYPE 类型名称 IS RECORD)
- type使用细则
- windows phone 手机解锁失败问题
- 打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件