VBA中常用技巧
2014-01-10 10:05
176 查看
常量定义
Public Const i as Integer = 1
自定义类型
Type mytype
i as Integer
b as Boolean
s as String
end Type
错误处理
①跳过出错语句,继续执行
On Error Resume Next
处理代码
On Error Goto 0
②执行错误处理
On Error Resume Next
处理代码
On Error Goto ErrorHandler
ErrorHandler:
错误处理代码
分支处理
Select case condition
case "value1"
详细处理
case "value2"
详细处理
case else
详细处理
End Select
循环处理
①For i = 1 to j (Step y)
详细处理
next i
②For each obj in objs
详细处理
next obj
③Do While / Until Condition
详细处理
Loop
或Do
详细处理
Loop While / Until Condition
函数可选参数定义
Function funs(Optional i as Interger) as String
详细处理
End Function
参数类型是Variant的场合,可用Ismissing(x)判断参数是否赋值
函数随机参数定义
Function funs(ParamArray arglist() as Variant) as Integer
End Function
函数有多个参数时,随机参数需定义在最后
变量显式定义
Option Explicit
所用变量必须事先定义,否则无效。一个好的变成习惯。
控件相关
①使用控件的MouseDown和MouseMove事件,实现在窗体内移动控件。
Private Sub Image1_MouseDown(Button, Shift, x, y)
OldX = x
OldY = y
Image1.Zorder 0
End Sub
x,y是选中控件的鼠标坐标值,Zorder使该图像位于最前面。
Private Sub Image1_MouseMove(Button, Shift, x, y)
If Button = 1 then
Image1.Left = Image1.Left + (X - OldX)
Image1.Top = Image1.Top + (Y - OldY)
End if
End Sub
Button=1意味着按下了鼠标左键。
Public Const i as Integer = 1
自定义类型
Type mytype
i as Integer
b as Boolean
s as String
end Type
错误处理
①跳过出错语句,继续执行
On Error Resume Next
处理代码
On Error Goto 0
②执行错误处理
On Error Resume Next
处理代码
On Error Goto ErrorHandler
ErrorHandler:
错误处理代码
分支处理
Select case condition
case "value1"
详细处理
case "value2"
详细处理
case else
详细处理
End Select
循环处理
①For i = 1 to j (Step y)
详细处理
next i
②For each obj in objs
详细处理
next obj
③Do While / Until Condition
详细处理
Loop
或Do
详细处理
Loop While / Until Condition
函数可选参数定义
Function funs(Optional i as Interger) as String
详细处理
End Function
参数类型是Variant的场合,可用Ismissing(x)判断参数是否赋值
函数随机参数定义
Function funs(ParamArray arglist() as Variant) as Integer
End Function
函数有多个参数时,随机参数需定义在最后
变量显式定义
Option Explicit
所用变量必须事先定义,否则无效。一个好的变成习惯。
控件相关
①使用控件的MouseDown和MouseMove事件,实现在窗体内移动控件。
Private Sub Image1_MouseDown(Button, Shift, x, y)
OldX = x
OldY = y
Image1.Zorder 0
End Sub
x,y是选中控件的鼠标坐标值,Zorder使该图像位于最前面。
Private Sub Image1_MouseMove(Button, Shift, x, y)
If Button = 1 then
Image1.Left = Image1.Left + (X - OldX)
Image1.Top = Image1.Top + (Y - OldY)
End if
End Sub
Button=1意味着按下了鼠标左键。
相关文章推荐
- VBA常用技巧代码解析
- EXCEL VBA常用到的技巧
- 40种网站设计常用技巧
- SAP几则常用的BASIS技巧整理
- C#常用技巧
- linux 常用命令及技巧
- mysql sql常用技巧
- HTML、CSS常用技巧
- IntelliJ IDEA 常用快捷键列表及技巧大全
- SHELL脚本编程的常识和VI常用技巧
- jQuery操作表格(table)的常用方法、技巧汇总
- delphi的一些常用操作技巧,持续更新中。。。
- linux环境下android常用工具和技巧
- Linux Shell常用技巧(十一) 交互式使用shell
- js常用技巧总结(转)
- Javascript中最常用的55个经典技巧
- array_map常用技巧
- Javascript中最常用的55个经典技巧
- DOS下常用网络命令技巧
- 你应该知道的CSS2.0中最常用的18条技巧