1、MsgBox: 将信息显示在屏幕上,有多个按钮供用户选择,选择按钮的结果的类型是VbMsgBoxResult

 

2、Application.InputBox: 接受用户输入,并对输入进行校验

 

3、iif: 三元操作符,语法iif(expr,turepart,falsepart) 根据expr的返回结果来确定返回后面两个中的一个值,如果为true则返回truepart,如果为false则返回falsepart

  如果有多个条件可以使用And Or来连接

 

4、条件判断语句

语法: if condition then statements

'如果statements有多句的话,需要用冒号将他们连接起来


语法:

if condition then

   statements

else

   statements
end if




多条件嵌套:

if condition then

   if condition then

     statements

   else

     statements

   end if

end if

 

5、select case: 相当于其他语言中的switch

sub time()

  tim =  hour(now)

  select case tim

  case 7 to 10

     msg = “上午”

  case 11,12

     msg=”中午”

  case 13 to 17:
  
     msg=”下午”

  case 18 to 23

     msg=”晚上”

  case 24,is<7

     msg=”午夜”

  end select

  msgbox “现在是: “ & msg

end sub

 

6、choose: 从参数列表中返回一个值,他只能用于返回值

 choose(3,1,2,3,4): 1234中返回第3个值,即3

 

7、for next

for item = 1 to 100 step 1

   sum += item

   ‘还可以做条件判断,使用exit for退出循环

next item

msgbox sum

 

8、for each next : 常用于循环对象集合

for each element in group

   statements

   exit for

next

 

9、do循环

do

  statements

  exit do
loop



‘while条件成立时才会执行循环

do while condition

   statements

   exit do

loop



‘直接条件成立时才停循环

do until condition

   statements

   exit do

loop



‘直到符合条件才才停止循环

do

   statements

   exit do

loop while condition



‘直到不符合条件时才停止循环

do

   statements

   exit do

loop unitl condition

 

10、err对象: vba的错误对象

    err.raise: 主动引起一个错误

   err.clear: 清除err对象的所有属性设置

   Error(arg): 获取arg所对应的错误信息或者创建一个错误

   on error goto line: 当出现错误时跳转到标签line

   on error resume next: 当出现错误时继续执行下一句

   on error goto 0: 禁止当前过程中任何已启动的错误处理程序,可以让上面两个语句失效

 

11、filedialog对象: 可以打开、另存为、文件选择、文件夹选择

 

12、application.getopenfilename: 创建具有筛选功能的文件选择对话框

原创文章,转载请注明出处:http://www.nwumba.cn/article/18/