乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>VBA</em> 中 <em>do</em> <em>

<em>VBA</em> 中 <em>do</em> <em>

作者:乔山办公网日期:

返回目录:excel表格制作


你这个AB列,填着公式么?
i=1
do while cells(i,1)<>"是" and cells(i,2)="上列不是""是""!"
i=i+1

loop

VBA 中Do while Loop用法如下e799bee5baa6e997aee7ad94e4b893e5b19e365
VBA中如果不知道重复多少次,使用 Do...Loop 语句。

Do...Loop 语句重复执行某段代码直到条件是 true 或条件变成 true。
重复执行代码直到条件是 true
使用 While 关键字来检查 Do... Loop 语句的条件。

Do While i>10
'some code
Loop

如果 i 等于 9,上述循环内的代码将终止执行。

Do
'some code
Loop While i>10

这个循环内的代码将被执行至少一次,即使 i 小于 10。
重复执行代码直到条件变成 true
使用 Until 关键字来检查 Do...Loop 语句的条件。

Do Until i=10
'some code
Loop

如果 i 等于 10,上述循环内的代码将终止执行。

Do
'some code
Loop Until i=10

这个循环内的代码将被执行至少一次,即使 i 等于 10。
退出 Do...Loop
可以通过 Exit Do 关键词退出 Do...Loop 语句。

Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop

这个循环内的代码,只要 i 不为 10 且 i 大于 10 时都将被执行。

扩展资料:

While 关键字用于检查 Do...Loop 语句中的条件。有两种方式检查条件:在进入循环之前检查条件(如下面的 ChkFirstWhile 示例);或者在循环至少运行完一次之后检查条件(如下面的 ChkLastWhile 示例)。

在 ChkFirstWhile 过程中,如果 myNum 的初始值被设置为 9 而不是 20,则永远不会执行循环体中的语句。在 ChkLastWhile 过程中,循环体中的语句只会执行一次,因为条件在检查时已经为 False。

Sub ChkFirstWhile()

Dim counter,myNum,counter = 0,myNum = 20

Do While myNum > 10,myNum = myNum - 1,counter = counter + 1

Loop

MsgBox 循环重复了 & counter & 次。

End Sub,Sub ChkLastWhile()

Dim counter,myNum

counter = 0,myNum = 9

Do  myNum = myNum - 1

counter = counter + 1

Loop While myNum > 10

MsgBox 循环重复了 & counter & 次。

End Sub

重复执行语句直到条件变为 True

参考资料:循环语句_百度百科


通常有下面一些循环语句:
1、For ... Next
如: For i=1 to 100
Cells(i,1)=1
Next
2、Do Until .... Loop
如: i=1
Do Until i=100
Cells(i,1)=i
i=i+1
Loop
3、Do Where ... Loop
如: i=1
Do Where i<=100
Cells(i,1)=i
i=i+1
Loop

Public Sub kkkk()
Dim i, j
i = 2
Do
k = 0
For j = 1 To 50
If Cells(i + j, 1) = Cells(i, 1) Then
k = k + 1
End If
Next j
Range("G" & i & ":G" & i + k).Select
Application.CutCopyMode = False
Selection.Copy
Range("H" & i).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
If k > 0 Then
Range("f" & i + 1 & ":j" & i + k).Select
Selection.ClearContents
End If
i = i + k + 1
Loop Until i > 576
End Sub

相关阅读

  • <em>VBA</em> 中 <em>do</em> <em>

  • 乔山办公网excel表格制作
  • 你这个AB列,填着公式么?i=1do while cells(i,1)<>"是" and cells(i,2)="上列不是""是""!"i=i+1loopExcel用VBA把这个DO循环改为FOR循环" src="/uploads/tu/762.jpg" style="width: 400px; h
关键词不能为空
极力推荐

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