定义变量、常量,赋值
'定义变量
Dim a,b
'赋值变量
a = 1
b = 2
'定义常量
const PI = 3.141592653
输入、输出
Dim input
'输入
input = inputbox("这是输入提示", "这是输入框的标题", "这是默认值")
'输出,不能用【+】进行连接,用【&】
msgbox("这是你输入的:"&input)
定义VB子程序
'定义子程序,子程序不会有返回值
Sub mySub(times)
for i=0 to times
msgbox("hello")
next
end Sub
'调用子程序
mySub(3)
定义VB函数程序
'定义VB函数程序,有返回值
Function myfunction(a,b)
myfunction=a+b
End Function
'调用函数程序
msgbox(myfunction(5,9))
条件语句
Dim input
input = inputbox("input number")
'第一种
if input > 100 then
msgbox(" > 100")
elseif input > 50 then
msgbox(" > 50")
else
msgbox(" <= 50")
end if
'第二种
select case input
case 1
msgbox("Monday")
case 2
msgbox("Tuesday")
end select
循环语句
'【for 循环】
'step表示每次+2
'exit for 退出循环
for i=0 to 10 step 2
if i>=8 then exit for
msgbox(i)
next
'【for each 循环】
Dim cars(3)
cars(0) = "car 1"
cars(1) = "car 2"
cars(2) = "car 3"
cars(3) = "car 4"
for each x in cars
msgbox(x)
next