VBA程序设计中while ..... wend 如何强制退出???

housemouse 2008-09-18 09:18:44
Private Sub CommandButton1_Click()
Dim TempY, TempZ As Integer
Dim SheBei, DanYuanGeZhi As String


TempY = 2

If (Trim(TextBox1.Text) <> "") Then
While (Not IsEmpty(Sheets("xuancheng").Cells(TempY, 19).Value))
If (TextBox1.Text = Sheets("xuancheng").Cells(TempY, 19).Value) Then
Sheets("xuancheng").Select
TextBox3.Text = Sheets("xuancheng").Cells(TempY, 1).Value
TextBox5.Text = Sheets("xuancheng").Cells(TempY, 11).Value
TextBox6.Text = Sheets("xuancheng").Cells(TempY, 12).Value
DanYuanGeZhi = Sheets("xuancheng").Cells(TempY, 1).Value
SheBei = Left(DanYuanGeZhi, Application.WorksheetFunction.Find("/P", DanYuanGeZhi, 1) - 1)
End If
TempY = TempY + 1
Wend
End If
Sheets("ip").Select
TempZ = 2
If (Trim(TextBox3.Text) <> "") Then
While (Not IsEmpty(Sheets("ip").Cells(TempZ, 1).Value))
If (SheBei = Sheets("ip").Cells(TempZ, 1).Value) Then
TextBox4.Text = Sheets("ip").Cells(TempZ, 9).Value
Exit Sub
End If
TempZ = TempZ + 1
Wend
End If
End Sub
第一个while循环到我找到记录之后我不想再往下面运行了直接退出循环如何实现?但是我不想退出整个SUB只是想退出循环,因为我还要执行下一个while循环。
...全文
2795 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangling111111 2008-09-20
  • 打赏
  • 举报
回复
用DO WHILE 吧,可用EXIT DO来退出
VB对循环支持得不好
yangkunjie 2008-09-19
  • 打赏
  • 举报
回复
while wend是vba中的语法,是早期vb支持的一种格式,类同while...end while
可以用exit while结束,摘录msdn资料如下
--------------------------------------
只要给定条件为 True 就运行一系列语句。


While condition
[ statements ]
[ Exit While ]
[ statements ]
End While


各部分说明
condition
必选。Boolean 表达式。如果 condition 为 Nothing,Visual Basic 会将其视为 False。

statements
可选项。跟在 While 后面的一个或多个语句,这些语句将在每次 condition 为 True 时运行。

Exit While
可选项。将控制权传送到 While 块外部。

End While
必选。结束 While 块的定义。
-------------------------------------


另外:也可以使用用goto语句找到符合条件后跳转

5,172

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧