Pages

Wednesday, March 21, 2012

A For-Next example with an Exit For statement

 

Sub exitfordemo()

    Dim maxval As Double

    Dim row As Long

    maxval = Application.WorksheetFunction.Max(Range("A:A"))

     For row = 1 To Rows.count

        If Range("a1").Offset(row - 1, 0).Value = maxval Then

            Range("a1").Offset(row - 1, 0).Activate

            MsgBox "max value is in row " & row

            Exit For

        End If

    Next row

End Sub

 

Sub fillrange()

    Dim col As Long

    Dim row As Long

    For col = 1 To 5

        For row = 1 To 12

            Cells(row, col) = Rnd

        Next row

    Next col

End Sub

Sub nestedloops()

    Dim myarray(10, 10, 10)

    Dim i As Integer

    Dim j As Integer

    Dim k As Integer

      For i = 1 To 10

        For j = 1 To 10

            For k = 1 To 10

                myarray(i, j, k) = 0

            Next

        Next

    Next

       

End Sub

 

No comments:

Post a Comment