VBA 中的各種迴圈

2022-03-10 21:38:49 字數 1971 閱讀 5453

利用迴圈和分支語句判斷 b 列數字的符號,將結果填入 c列。

原始**如如下:

1、for 迴圈和 if 語句,**如下:

sub

sign()

dim sign as

string, i as

integer

'宣告變數

for i = 3

to13

step

1if cells(i, "

b") < 0

then

sign = "負數"

elseif cells(i, "

b") > 0

then

sign = "正數"

else

sign = "零"

endif

cells(i, "c

") = sign '

向 c 列對應單元格中寫入資料

next

iend sub

執行**,結果如下:

2、for 迴圈和 select case 語句,**如下:

sub

sign()

dim sign as

string, i as

integer

'宣告變數

for i = 3

to13

step

1select

case cells(i, "b"

)

case

is< 0

sign = "負數"

case

is > 0

sign = "正數"

case

else

sign = "零"

endselect

cells(i, "c

") =sign    '向 c 列對應單元格中寫入資料

next

i end sub

3、do  while 迴圈和 select case 語句, **如下:

subsign()

dim sign as string, i as integer      '宣告變數

i = 3    '

初始化迴圈變數

do while cells(i, "b") <> ""

select case cells(i, "b")

case is < 0sign = "負數"

case is > 0sign = "正數"

case elsesign = "零"

end selectcells(i, "c") =sign      '向 c 列對應單元格中寫入資料

i = i + 1      '更新哨兵變數

loop

end sub

4、do until 迴圈和 select case 語句, **如下:

subsign()

dim sign as string, i as integer      '宣告變數

i = 3      '初始化迴圈變數

do until cells(i, "b") = ""

select case cells(i, "b")

case is < 0sign = "負數"

case is > 0sign = "正數"

case elsesign = "零"

end selectcells(i, "c") =sign

i = i + 1      '更新迴圈變數

loop

end sub

vba 跳出for迴圈 VBA的基本語句

塊 的形式的vba語句比完整一行的 功能和適用性方面更強。二選一 if 條件表示式 then 條件表示式返回true時要執行的操作和計算 else 條件表示式返回false時要執行的操作和計算 end if 多選一 if 條件表示式 then 條件表示式返回true時要執行的操作和計算 elseif...

vba中字典迴圈時的報錯

本錯誤 問題與字典相關,因為如果是一般的for i 1 to x 這種迴圈未出現問題.同乙個存在於某固定行數的for迴圈體系如果沒跑完不能回到之前並繼續進行同乙個 存在於某固定行數 的迴圈,否則報錯 該陣列被固定或暫時鎖定,英文 this array is fixed or temporarily ...

LUA中各種迴圈

1 while迴圈 while true do end2 for的多種迴圈 數值for迴圈 for 起始值,上限,step do for i 1,5,1 do print i end 泛型for迴圈,迭代陣列 迭代陣列中每個鍵值,in後面跟的是迭代器,可以自定義迭代器ipairs pairs都內建的...