|
本帖最后由 JunJun2013 于 3-6-2014 13:56 编辑
通过系统录制宏后,点编辑, 可以参看宏的VBA语言,可以复制应用到新的程序中,这个是最实用的上手办法了。
毕竟那么多功能和定义,一下子都掌握难。
一个最基本的程序是这样的:
- 用撇号描述程序的内容,比如 ‘this is for testing
- 定义变量,比如dim total as long(或者integer)
- 功能或任务,最基本的: +,-,*,/,=,<=,>=,<>, &, msgbox, msgbox(msg, vbYesNo) ," " 给予文本数值,# # 给予日期值,比如#03/06/2014#
还有这个^,表示求幂,计算优先顺序同常理,用()来改变默认优先顺序
- 结构: 比如,If-Then-Else 或者 loop
不用管大小写和空格,回车后自动修正。
例1:--运行这个可以得到101,这里的结构就是loop
Sub testing()
'testing
Dim total As Long, i As Long
total = 0
For i = 0 To 100
total = total + 1
Next i
MsgBox total
End Sub
例2:--询问对话框,这个例子中有关于信息框和如何分配单位格数值的表达(入门必备)。
Sub msgtesting()
Msg = "is your name " & Application.Workbooks("test.xlsm").Worksheets("sheet1").Range("A1") & "?"
Ans = MsgBox(Msg, vbYesNo)
If Ans = vbNo Then
MsgBox "oh, never mind"
Else: MsgBox "I must be clairvoyang"
End If
End Sub
|
|