如:怎样让循环变量取0、1、3这三个值

解决方案 »

  1.   

    Dim i as integer 
    for i=0 to 3
        if i=2 then
            i=i+1
        endif
    next i
      

  2.   

    For i = 0 To 3
    '这里写正常代码
    Debug.Print i
    '……
    If i = 1 Then i = 2
    Next
      

  3.   

    dim i as integer
    for i=0 to 3
    if i<>2 then
    '代码
    end if
    next i
    霸王和bulebeer的都有问题
    i的数值被改变了,跳过了下也个数!!
      

  4.   

    Private Sub Command1_Click()
        Dim buff() As Long
        ReDim buff(2)
        buff(0) = 0
        buff(1) = 1
        buff(2) = 3
        Dim i As Long
        For i = 0 To 2
            '下面用buff(i)替换循环体中的i
        Next
        Erase buff
    End Sub
      

  5.   

    to mccxj(爱吃饺子) ,你的办法也可以,但是你的循环被执行的次数仍然是4次,有一次空循环而已,不可取rainstormmaster(暴风雨 v2.0)的才具有通用性,是个好主意
      

  6.   

    Private Sub Command1_Click()
    Dim s As String
    s = "013"
    For i = 0 To 1000
    DoEvents
    k = Val(Mid(s, (i Mod 3) + 1, 1))
    Debug.Print k
    Next
    End Sub
      

  7.   

    要不这样吧:
    .....
    dim SearchString as String
    SearchString="0,2,3"
    for xx=StartValue to EndValue
      if instr(SearchString,str(xx))>0 then
         .....
      end if
    next
    ......
      

  8.   

    Private Sub Command1_Click()
    Dim i As Integer, j As Integer
    For i = 0 To 100
    j = 2 ^ (i Mod 3) - 1
    Debug.Print j
    Next
    End Sub
      

  9.   

    for i=0 to 3
        if i<>2 then
            此处加处理代码
        endif
    next i在FOR循环里并不能改变FOR的初始值。但可以在时间处理时过滤掉i=2时的事件。
      

  10.   

    唉,让rainstormmaster(暴风雨 v2.0)说了。
    这个办法是最正规的,因为可扩展啊。不用多说了,想一下就知道了。
      

  11.   

    //for i=0 to 3
        if i<>2 then
            此处加处理代码
        endif
    next i
    不建议用类似的代码,原因是:假如我把问题:
    //怎样让循环变量取0、1、3这三个值
    修改为
    //怎样让循环变量取0、29734、42111这三个值
    那按照上面的方法,岂不是要执行4万多次?
      

  12.   

    对暴风雨的代码做了修改,呵呵!
    Private Sub Command1_Click()
        Dim buff() As Long
        ReDim buff(2)
        buff(0) = 0
        buff(1) = 1
        buff(2) = 3
        Dim i
        For Each i In buff()
            Debug.Print i
        Next
        Erase buff
    End Sub
      

  13.   

    实现的方法好多,来一个DO+Switch看看!
    Private Sub Command1_Click()
        Dim j As Long
        Static i As Long
        Do
            j = Switch(i = 0, 0, i = 1, 1, i = 2, 3)
            Debug.Print j
            i = i + 1
        Loop While j < 3
    End Sub
      

  14.   

    0、1、3 或 0、29734、42111这三个值都为数列,必能写成通项公式 An = F(n)
    所以:
    for i=1 to 3
        '下面用F(i)替换循环体中的i
    next i
      

  15.   

    最笨的方法:Dim i as integer 
    for i=0 to 3
        if i=0 or i=1 or i=3 then
            
        endif
    next i
      

  16.   

    刚才忘记把代码附上来了,很简单的!呵呵Dim Str As String
    Str = "013"
    For i = 1 To Len(Str)
        Debug.Print Mid(Str, i, 1)
    Next
      

  17.   

    rainstormmaster(暴风雨 v2.0) ( ) 的方法不错!
      

  18.   

    奇怪的问题,如果循环变量的值要按自已的要求取值,何不用For each?如:
      Dim a(1 To 3) As Integer, k As Integer
      a(1) = 0: a(2) = 1: a(3) = 3
      For Each k In a
       Print k
      Next k
      

  19.   

    flyoutmouse(笨笨)的方法好啊,我第一次见,惭愧~~~
      

  20.   

    Visual Basic Language Specification   10.9.3 For Each...Next StatementsSee Also
    10.9.1 While...End While and Do...Loop Statements | 10.9.2 For...Next Statements | 10.9 Loop Statements | For Each...Next Statements (Visual Basic Language Reference) | For Each...Next Statements (Visual Basic Language Concepts)
    A For Each...Next statement loops based on the elements in an expression. A For Each statement specifies a loop control variable and an enumerator expression.The loop control variable is specified either through an identifier followed by an As clause or an expression. In the case of an identifier, the identifier defines a new local variable of the type specified in the As clause, scoped to the entire For Each loop. In the case of an expression, the expression must be classified as a variable. The enumerator expression must be classified as a value and its type must be a collection type. An implicit conversion must exist from the element type of the collection to the type of the loop control variable.The loop control variable cannot be used by another enclosing For Each statement. A For Each statement must be closed by a matching Next statement. A Next statement without a loop control variable matches the innermost open For Each. A Next statement with one or more loop control variables will, from left to right, match the For Each loops that have the same loop control variable. If a variable matches a For Each loop that is not the most nested loop at that point, a compile-time error occurs.A type C is said to be a collection type if it implements the interface System.Collections.IEnumerable or if all of the following are true: C contains an accessible instance method with the signature GetEnumerator() that returns a type E. 
    E contains an accessible instance method with the signature MoveNext() and the return type Boolean. 
    E contains an accessible instance property named Current that has a getter. The type of this property is said to be the element type of the collection type. 
    Following is an example of a class that can be enumerated:Public Class IntegerCollection
        Private integers(10) As Integer    Public Class IntegerCollectionEnumerator
            Private collection As IntegerCollection
            Private index As Integer = -1        Friend Sub New(ByVal c As IntegerCollection)
                collection = c
            End Sub        Public Function MoveNext() As Boolean
                index += 1            Return index <= 10
            End Function        Public ReadOnly Property Current As Integer
                Get
                    If index < 0 OrElse index > 10 Then
                        Throw New System.InvalidOperationException()
                    End If                Return integers(index)
                End Get
            End Property
        End Class    Public Sub New()
            Dim i As Integer        For i = 0 To 10
                integers(i) = I
            Next i
        End Sub    Public Function GetEnumerator() As IntegerCollectionEnumerator
            Return New IntegerCollectionEnumerator(Me)
        End Function
    End Class
    Before the loop begins, the enumerator expression is evaluated and cast to System.Collections.IEnumerable if it does not satisfy the requirements above. GetEnumerator is called on the resulting value and the return value of the function is stored in a temporary. Then at the beginning of each loop, MoveNext is called on the temporary. If it returns False, the loop terminates. If it returns True, the Current property is retrieved, coerced to the type of the iterator variable (regardless of whether the conversion is implicit or explicit), and assigned to the iterator variable; then the loop block executes. When the Next statement is reached, execution returns to the top of the loop. If a variable is specified after the Next keyword, it must be the same as the first variable after the For Each. For example, consider the following code:Imports SystemModule Test
        Sub Main()
            Dim i As Integer
            Dim c As IntegerCollection = New IntegerCollection()        For Each i In c
                Console.WriteLine(i)
            Next i
        End Sub
    End Module
    It is equivalent to the following code:Imports SystemModule Test
        Sub Main()
            Dim i As Integer
            Dim c As IntegerCollection = New IntegerCollection()        Dim e As IntegerCollectionEnumerator        e = c.GetEnumerator()
            While e.MoveNext()
                i = e.Current            Console.WriteLine(i)
            End While
        End Sub
    End Module
    If the type E of the enumerator implements System.IDisposable, then the enumerator is disposed upon exiting the loop by calling the Dispose method. This ensures that resources held by the enumerator are released. If the method containing the For Each statement does not use unstructured error handling, then the For Each statement is wrapped in a Try statement with the Dispose method called in the Finally to ensure cleanup.Note   The System.Array type is a collection type, and since all array types derive from System.Array, any array type expression is permitted in a For Each statement. For single-dimensional arrays, the For Each statement enumerates the array elements in increasing index order, starting with index 0 and ending with index Length - 1. For multidimensional arrays, the indices of the rightmost dimension are increased first.
    For example, the following code prints 1 2 3 4:Imports SystemModule Test
        Sub Main()
            Dim x(,) As Integer = { { 1, 2 }, { 3, 4 } }
            Dim i As Integer        For Each i In x
                Console.Write(i & " ")
            Next i
        End Sub
    End Module
    It is not valid to branch into a For Each statement block from outside the block.ForEachStatement ::=
       For Each LoopControlVariable In Expression StatementTerminator
       [ Block ]
       Next [Expression ] StatementTerminatorcopy from MSDN Library