全民一起VBA基础篇第三课:变量与常量

变量

在这里插入图片描述

Sub 加法变量()
i = Cells(2, 2)
Cells(i, 9) = Cells(i, 5) + Cells(i, 7) '利用变量来控制
End Sub

计算半径,面积和体积

Sub 求面积和体积()
Radius = Cells(4, 3)
r = Radius
Square = 4 * r * r * 3
Cells(4, 4) = Square
volume = 4 / 3 * r * r * r * 3
Cells(4, 5) = volume '大小写不敏感

End Sub

在这里插入图片描述

常量

Option Explicit '强制申明,变量只能用一次
Sub 求面积和体积()
Dim Radius, r, square, volume
Const pi = 3.14 '申明常量,不允许变动
Radius = Cells(4, 3)
r = Radius
square = 4 * r * r * pi
Cells(4, 4) = square
volume = 4 / 3 * r * r * r * pi
Cells(4, 5) = volume '大小写不敏感
End Sub

发布了26 篇原创文章 · 获赞 5 · 访问量 1108

猜你喜欢

转载自blog.csdn.net/qq_43568982/article/details/103738118