乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>Excel</em> VBA 多重循环,如何在达到指定值<em>跳出循环&l

<em>Excel</em> VBA 多重循环,如何在达到指定值<em>跳出循环&l

作者:乔山办公网日期:

返回目录:excel表格制作


如果是Do循环,简单的借鉴下:

do while i>1 '当i>1的时候,循环体开始执zhidao
i=i+1 ‘每次让i增加一个单
if i>20 then ’增加i后判断i是否大于20
exit do ‘如果大于20,则跳出循环(exit do)
end if '不是,则绕出判断语句,继续执行循环
loop ’循环往复

一般来说,需要遍历的不需要跳出,比如从头到尾都需要计算,就不需要跳出。而如果是查找某个值,当找到后,就没有必要再循环下去了,所以要跳出。 具体还要根据实际情况来确定。
n=1 'i从1开始,到多少结束?
for i=1 to n
if len(cells(i,1))=0 then
for j=1 to 5
cells(i,j)=j
next j
else
for j=1 to 5
cells(i+1,j)=j
next j
end if
next i
这个循环实现容易,可你这个算法是有问题的,如果第一行非空,那么第二行被赋值,肯定也就非空了。这个值12345不是在第一行就是在第二行。

由于没有e799bee5baa6e79fa5e98193e58685e5aeb9336数据,只能大致写了下,你测试运行下吧

代码如下:

Sub 计算折扣()
    Dim sh As Worksheet
    Set sh = Worksheets("sheet1")
    Dim i As Double
    Dim n As Integer
    Dim m As Integer
    Dim o As Integer
    i = Sheets("1").[T2]
    If Range("A5") <> "" Then
        For o = 5 To 45
            For m = 5 To 45
                For n = 5 To 45
                    If sh.Cells(m, 12) <> "" Then
                        sh.Cells(n, 11) = sh.Cells(n, 5) * sh.Cells(n, 10)
                        sh.Cells(m, 12) = sh.Cells(m, 11)
                        sh.Cells(n, 13) = sh.Cells(n, 12) * i
                    Else
                        sh.Cells(m, 12) = ""
                    End If
                Next
            Next
        Next
    Else
        MsgBox "A5没有数据"
        Exit Sub
    End If
End Sub

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网