sub test(
) .....
endsub
function test(
) test =
123end
function
sub hello1(
)' h = ha(100)
' debug.print h
' call ha(120)
' ha (130)
test1
call test1
endsub
function ha(via as
integer
)debug.print
"hello"
ha = via
endfunction
sub test1(
)debug.print
"test1----"
endsub
for i =1to
10step
2 msgbox i
next i
private
sub constant_demo_click2(
)'fruits is an array
fruits =
array
("蘋果", "橙子", "櫻桃")
dim fruitnames as variant
'iterating using for each loop.
for each item in fruits
fruitnames = fruitnames & item &
chr(10)
next item
msgbox fruitnames
endsub
do
while i <
100'這裡處理邏輯 '
if i =
20then
i =100'利用while的破壞條件退出迴圈 '
endif
loop
private
sub whiletest(
) i =0do
i = i +
1 msgbox "the value of i is : "
& i loop
while i <
3end
sub
迴圈的跳出機制
name
="vba"
ifname
="vba"
then
msgbox "yes"
elseif
name
="***"
then
msgbox "no"
else
msgbox "x"
endif
private
sub switch_demo_click(
)dim myvar as
integer
myvar =
7select case myvar
case
1 msgbox "the number is the least composite number"
case
2 msgbox "the number is the only even prime number"
case
3 msgbox "the number is the least odd prime number"
case
else
msgbox "unknown number"
endselect
endsub
exit subiif
函式類似其他語言中的三目運算子,用來替代簡單的if--else
邏輯
exit function
exit for
exit dogoto 指定跳轉
sub t1(
)dim x as
integer
dim sr
100:
iflen
(sr)=0
orlen
(sr)=5
then
goto
100end
sub
gosub returnsub t2(
)dim x as
integer
for x =1to
10if cells(x,1)
mod2=0
then
gosub
100next x
exit
sub100
: cells(x,1)
= "偶數"
return
endsub
on error reusme nextsub t3(
)on error
resume
next
dim x as
integer
for x =1to
10'這裡如果不是數值導致計算出錯的話,會自動進行下一次的迴圈
cells(x,3)
= cells(x,2)
* cells(x,1)
next x
endsub
on error goto 100sub t4(
)on error
goto
100dim x as
integer
for x =1to
10 cells(x,3)
= cells(x,2)
* cells(x,1)
next x
exit
sub100
: msgbox "在第" & x & "行出錯了"
endsub
on error goto 0 取消錯誤跳轉sub t5(
)on error
resume
next
dim x as
integer
for x =1to
10if x >
5then
on error
goto
0 cells(x,3)
= cells(x,2)
* cells(x,1)
next x
exit
subend
sub
前面的幾個例子中的exit sub
很關鍵能夠避免指定**的重複執行
不相鄰的單元格區域range("d1:d3,f1:f3")
sheet1.delete
range(
"a1"
).copy destination:
=cells(1,
2)
range(
"a1"
).interior.colorindex =
3range(
"a1"
).font.colorindex =
3
'數字
dim x as
integer
'物件dim rg as range
set rg = range(
"a1"
)'陣列
dim arr(1to
10)asinteger
'模組1 aa 和 cc 都只能在 sub 中進行賦值,不能在外面
dim aa as
integer
public cc as
integer
sub hello1(
) aa =
11 cc =
12end
sub
'模組2 中可以訪問到 cc ,但是不能訪問 aa
sub test22(
)debug.print aa
debug.print cc
endsub
sub test(
) hah = evaluate(
"=sum(a1:b1)"
) range(
"d1")=
"=sum(a1:b1)"
debug.print hah
debug.print range(
"d1"
)end
sub
'借用 excel 函式
range(
"d8"
)"d1:d3"))
'借用 vba 函式
range(
"c4"
)= vba.instr(range(
"a4"
), range(
"b4"
))
Excel VBA程式設計 01 入門
在excel種需要開啟 開發工具 和 啟用所有巨集 兩點 開啟開發工具選項 巨集啟用 選單欄才會有開發工具選項 點選visualbasic,編寫第乙個vba程式 右鍵新建乙個模組以編寫程式 第乙個vba程式 sub 第乙個vba程式 dim 姓名 as string 姓名 inputbox 請輸入你...
Swif基礎語法01
import foundation 1,第乙個swift程式 println hello,world 2,定義常量 let a 10 let cinteger double 12.00 3,定義變數 var b 0 b 11 var bfloat float bfloat 11.0 println ...
基礎語法隨記01
保留字即關鍵字,我們不能把它們用作任何識別符號名稱。python 的標準庫提供了乙個 keyword 模組,可以輸出當前版本的所有關鍵字 import keyword keyword.kwlist false none true and as assert break class continue ...