📄 vbs581.htm
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML><HEAD><TITLE>For Each...Next Statement</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Language Reference">
<META NAME="Keywords" CONTENT="In,For,Each,Next,loops,Exit For,program control,control structures,repeating instructions,instructions repeating,arrays repeating instructions,collections repeating instructions,instructions,arrays,collections"><META NAME="Description" CONTENT="For Each...Next Statement"></HEAD><BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>For Each...Next Statement</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstoc.htm">Language Reference</A> <BR>
<A HREF="vbs16.htm">Version 2</A> <P></FONT>
</TD></TR>
</TABLE>
<FONT SIZE=2><P><A HREF="vbs582.htm">See Also</A></FONT>
<HR NOSHADE SIZE=1>
<H5>Description</H5>
<BLOCKQUOTE>Repeats a group of statements for each element in an <A HREF="vbs0.htm#defArray">array</A> or <A HREF="vbs0.htm#defCollection">collection</A>.</BLOCKQUOTE>
<H5>Syntax</H5>
<BLOCKQUOTE><b>For Each</b> <i>element </i><b>In</b> <i>group</i><BR>
[<i>statements</i>]<BR>
[<b>Exit For</b>]<BR>
[<i>statements</i>]<BR>
<b>Next</b> [<i>element</i>]<P>
The <b>For Each...Next</b> statement syntax has these parts:<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>element</i></FONT></TD>
<TD><FONT SIZE=2><A HREF="vbs0.htm#defVariable">Variable</A> used to iterate through the elements of the collection or array. For collections, <i>element</i> can only be a <b>Variant</b> variable, a generic <b>Object</b> variable, or any specific <A HREF="vbs0.htm#defOLEAutomationObject">Automation object</A> variable. For arrays, <i>element</i> can only be a <b>Variant</b> variable.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>group</i></FONT></TD>
<TD><FONT SIZE=2>Name of an object collection or array.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>statements</i></FONT></TD>
<TD><FONT SIZE=2>One or more statements that are executed on each item in <i>group</i>.</FONT></TD></TR></TABLE></BLOCKQUOTE>
<H5>Remarks</H5>
<BLOCKQUOTE>The <b>For Each</b> block is entered if there is at least one element in <i>group</i>. Once the loop has been entered, all the statements in the loop are executed for the first element in <i>group</i>. As long as there are more elements in <i>group</i>, the statements in the loop continue to execute for each element. When there are no more elements in <i>group</i>, the loop is exited and execution continues with the statement following the <b>Next</b> statement.<P>
The <b>Exit For</b> can only be used within a <b>For Each...Next</b> or <b>For...Next</b> control structure to provide an alternate way to exit. Any number of <b>Exit For</b> statements may be placed anywhere in the loop. The <b>Exit For </b>is often used with the evaluation of some condition (for example, <b>If...Then</b>), and transfers control to the statement immediately following <b>Next</b>.<P>
You can nest <b>For Each...Next</b> loops by placing one <b>For Each...Next</b> loop within another. However, each loop <i>element</i> must be unique.
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=87%><TR><TD COLSPAN=2 VALIGN=BOTTOM><hr noshade size=1></TD></TR><TR>
<TD VALIGN=TOP><FONT SIZE=2><b>Note</b> If you omit <i>element</i> in a <b>Next</b> statement, execution continues as if you had included it. If a <b>Next</b> statement is encountered before its corresponding <b>For</b> statement, an error occurs.</FONT></TD></TR>
<TR><TD COLSPAN=2 VALIGN=TOP><hr noshade size=1></TD></TR></TABLE></BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -