设置主页 | 收藏本站 Excel学习网-公益型的EXCEL在线学习网站,助您轻松办公!

vba do loop的几个例子

2026年09月05日 17:16 发布来源:Excel学习网
    一、Do…Loop语句的语法
    Do[While I Until<逻辑表达式>]
    <循环体>
    Loop[While I Until<逻辑表达式>]
    当逻辑表达式的值为True时,使用While关键字执行循环体,直到逻辑表达式的值为False时跳出循环体,即执行Do While-Loop循环语句。
    当逻辑表达式的值为False时,使用Until关键字执行循环体,直到逻辑表达式的值为True时跳出循环体,即执行Do Until-Loop循环语句。
    在大多数情况下Do while.Loop循环语句与Do Until—Loop循环语句可以互换使用,只需将循环条件取反即可。但对于需要先判断再执行的一些操作,最好使用Do While-Loop循环语句。
    另外,使用Do-Loop循环语句时,需要在循环体内使用“Exit Do”语句跳出Do-Loop循环,进而执行Loop后面的一条语句。
    二、do…loop的几个例子
    ①例子
    Dim myCnt As Long
    myCnt = 1
    Do While myCnt <= 5
    Cells(myCnt, 3).Value = Cells(myCnt, 1).Value * Cells(myCnt, 2).Value
    myCnt = myCnt + 1
    Loop
    当myCnt小于等于5时循环,否则就退出循环。
    ②例子
    Dim myCnt As Long
    myCnt = 1
    Do
    Cells(myCnt, 3).Value = Cells(myCnt, 1).Value * Cells(myCnt, 2).Value
    myCnt = myCnt + 1
    Loop While myCnt<=5
    ③例子
    Dim myCnt As Long
    myCnt = 1
    Do
    Cells(myCnt, 3).Value = Cells(myCnt, 1).Value * Cells(myCnt, 2).Value
    myCnt = myCnt + 1
    If myCnt>5 Then Exit Do
    Loop
    ④例子
    counter = 0
    myNum = 9
    Do Until myNum = 10
    myNum = myNum - 1
    counter = counter + 1
    If myNum < 10 Then Exit Do
    Loop
    MsgBox "The loop made " & counter & " repetitions."
原创文章如转载请注明:转载自Excel学习网 [ https://www.excelxxw.com/]
需要保留本文链接地址:https://www.excelxxw.com/jiqiao/2665.html
上一篇:excel中的Edate怎么使用 下一篇:返回列表
网站地图 | XML地图 | 免责声明 | 关于我们 | Excel学习网:优秀的EXCEL在线学习和资源分享网站。
版权所有: CopyRight © 2013-2026 www.excelxxw.com All Rights Reserved.
豫ICP备2024075255号 联系我们:12162961@qq.com