vba 中的循环和退出循环-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > vba >

vba 中的循环和退出循环

作者:迹忆客 最近更新:2023/05/09 浏览次数:

计算机编程中的循环非常重要。它们允许用最少的代码行执行重复性任务。取决于要执行的任务,计算机中对循环的需求有几个原因。

尽管它们非常强大,但它们在上下文和语法上都很简单。

在 vba 中,可以使用三个循环。

vba for 循环

语法:

for [ counter ] = [start] to [end]  step [ step ]
    [ statements ]
    [ exit for ]
next [ counter ]

参数:

   
[ counter ] 必需的。通常是一个整数或长变量,用于保存计数器的当前值
[ start ] 必需的。计数器的起始值
[end] 必需的。计数器的最终值
[ statements ] 在循环内执行的代码块
[ exit ] 可选的。强制循环停止并退出循环代码块
[ step ] 可选的。每次迭代计数器的增量。如果未声明,则值为 1

for 循环示例:

该代码块将接受一个整数值作为其起始编号,然后使用 for 循环将该数字减小到零。它将打印每次迭代的结果。

sub reducetozero(numstart as integer)
dim i as integer
for i = numstart to 0 step -1
    debug.print "the number is now " & i
    if i = 0 then
        debug.print ("done!")
    end if
next i
end sub
sub testsub()
call reducetozero(10)
end sub

输出:

the number is now 10
the number is now 9
the number is now 8
the number is now 7
the number is now 6
the number is now 5
the number is now 4
the number is now 3
the number is now 2
the number is now 1
the number is now 0
done!

vba do until 循环

语法:

do until [condition]
    [statements]
    [exit do]
loop

参数:

   
[condition] 必需的。一旦为真将退出代码块的条件
[statements] 必需的。要执行的代码块
[exit do] 可选的。强制循环停止并退出代码块

do until 循环示例:

该代码块将接受一个整数值作为其起始编号,然后使用 do until 循环将数字增加到 10。它将打印每次迭代的结果。

sub incrementtoten(numstart as integer)
dim i as integer
i = numstart
do until i = 10   1
    debug.print "the number is now " & i
    i = i   1
loop
end sub
sub testsub()
call incrementtoten(-5)
end sub

testsub 输出:

the number is now -5
the number is now -4
the number is now -3
the number is now -2
the number is now -1
the number is now 0
the number is now 1
the number is now 2
the number is now 3
the number is now 4
the number is now 5
the number is now 6
the number is now 7
the number is now 8
the number is now 9
the number is now 10
done!

vba do while 循环

语法:

do while [condition]
    [statements]
    [exit do]
loop

参数:

   
[condition] 必需的。执行代码块需要为真的条件。
[statements] 必需的。要执行的代码块
[exit do] 可选的。强制循环停止并退出代码块

do while 循环示例:

该代码块将接受一个整数值作为其起始编号,然后使用 do while 循环将该数字增加到 10。它将打印每次迭代的结果。

sub incrementtoten(numstart as integer)
dim i as integer
i = numstart
do while i < 10   1
debug.print "the number is now " & i
i = i   1
loop
debug.print "done!"
end sub
sub testsub()
call incrementtoten(-9)
end sub

testsub 输出:

the number is now -9
the number is now -8
the number is now -7
the number is now -6
the number is now -5
the number is now -4
the number is now -3
the number is now -2
the number is now -1
the number is now 0
the number is now 1
the number is now 2
the number is now 3
the number is now 4
the number is now 5
the number is now 6
the number is now 7
the number is now 8
the number is now 9
the number is now 10
done!

在 vba 中使用 exit 命令强制停止循环

在处理循环时,它们与循环在完成时将继续执行的条件绑定。然而,一个常见的场景是我们需要退出循环,即使条件仍然满足。一个典型的应用是在处理错误处理方法时,搜索时停止技术。需要立即退出循环以防止进一步的错误或节省执行时间。

下面的代码块将演示使用 exit 命令强制循环在 do 循环中停止。

此代码块旨在在两个数字的商为 1 时停止循环。这两个数字是在子程序的参数 upperboundlowerbound 整数值之间随机生成的。如果生成的除数 (divis) 为零,则会发生错误,因为不允许除以零。为避免这种情况发生,我们将使用 exit 命令终止循环。

sub stopwhenquotientisone(upperbound as integer, lowerbound as integer)
dim divid as integer
dim divis as integer
dim isone as boolean
divid = int((upperbound - lowerbound   1) * rnd   lowerbound)
divis = int((upperbound - lowerbound   1) * rnd   lowerbound)
do until isone = true
    if divis = 0 then
        debug.print "illegal divisor. exiting the loop"
        exit do
    end if
debug.print divid / divis
if divid / divis = 1 then
    isone = true
    debug.print "done! one is achieved."
end if
divid = int((upperbound - lowerbound   1) * rnd   lowerbound)
divis = int((upperbound - lowerbound   1) * rnd   lowerbound)
loop
end sub
sub testsub()
call stopwhenquotientisone(4, -4)
end sub

testsub 输出(第一次试):

-4 
-0 
-0.666666666666667 
illegal divisor. exiting the loop

testsub 输出(第四次试):

-2 
 0 
-3 
 1 
done! one is achieved.

下面的代码块将演示使用 exit 命令强制循环在 for 循环中停止。

在这个例子中,stopwhennegative 子程序需要两个参数。首先是 startnum,然后是 endnum。循环将从 startnum 迭代到 endnum。如果计数器变为负数,则循环将由 exit 命令终止。

sub stopwhennegative(startnum as integer, endnum as integer)
dim i as integer
 
for i = startnum to endnum step -1
 
    if i < 0 then
        debug.print "opps, negative values detected. exiting loop."
        exit for
    end if
debug.print "the current number is :" & i
next i
end sub
sub testsub()
call stopwhennegative(10, -5)
end sub

testsub 输出:

the current number is :10
the current number is :9
the current number is :8
the current number is :7
the current number is :6
the current number is :5
the current number is :4
the current number is :3
the current number is :2
the current number is :1
the current number is :0
opps, negative values detected. exiting loop.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 vba 中计算列数

发布时间:2023/03/19 浏览次数:599 分类:vba

本教程演示如何计算 vba 中的列数。

发布时间:2023/03/19 浏览次数:627 分类:vba

本教程演示了如何在 vba 中使用具有多个条件的自动过滤器。

发布时间:2023/03/19 浏览次数:383 分类:vba

本教程演示如何在 vba 中设置工作表。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图