3.7
学习内容:如何用VBA,编写一个加法小程序
步骤:
1.在mac excel2017中,打开VBA的开发者模式


2.加入数字,点击按钮,更改按钮名称

3.VBA代码
重点:单元格表示:cells(行,列)
代码
如何实现不同的加减乘除:
Sub 做加法()
Cells(11, 8) = "+"
Cells(11, 11) = Cells(11, 7) + Cells(11, 9)
End Sub
Sub 做减法()
Cells(11, 8) = "-"
Cells(11, 11) = Cells(11, 7) - Cells(11, 9)
End Sub
4.学习用变量
变量:要有意义,尽量英文or拼音,数字不能做首位
Sub 球()
x = Cells(6, 1)
area = pi * x * x
tiji = (4 / 3) * pi * x * x * x
Cells(6, 2) = area
Cells(6, 3) = tiji
End Sub
5.学习对全文进行强制声明以及常量规定
Option Explicit【强制声明,登记注册】
Dim x, area, tiji【规定,下文只使用这些变量】
Const pi = 2【常量声明】
6.循环语句
Option Explicit
Dim rate, i
Sub 人民币兑换美元()
rate = Cells(6, 8)
For i = 9 To 12(正常来说,step是加1,所以省略,如果是倒叙,需要是step-1)
Cells(i, 7) = Cells(i, 7) / rate
Next i
Cells(8, 7) = "美元"
End Sub
7.if语句
注意:右边赋值给左边,所以,如果是先设定的i,之后还要讲i设定给单元。即:cells(6,7)=i
网友评论