Понедельник, Май 25, 2020

Циклы For - Next

Sub SumSquareRoots()
   Dim Sum As Double
   Dim Count As Integer
   Sum = 0
   For Count = 0 To 100
      Sum = Sum + Sqr(Count)
   Next Count
   MsgBox Sum
End Sub


Sub SumSquareRoots()
   Dim Sum As Double
   Dim Count As Integer
   Sum = 0
   For Count = 1 To 100 Step 2
      Sum = Sum + Sqr(Count)
   Next Count
   MsgBox Sum
End Sub


 

Sub DeleteRows()
   Dim RowNum As Long
   For RowNum = 10 To 2 Step -2
      Rows(RowNum).Delete
   Next RowNum
End Sub


 

Sub GoodLoop()
   Dim StartVal As Integer
   Dim NumToFill As Integer
   Dim Cnt As Integer
   StartVal = 1
   NumToFill = 100
   For Cnt = 0 To NumToFill - 1
      ActiveCell.Offset(Cnt, 0).Value = StartVal + Cnt
   Next Cnt
End Sub


 

Sub ExitForDemo()
   Dim MaxVal As Double
   Dim Row As Long
   MaxVal = Application.WorksheetFunction.Max(Range("A:A"))
   For Row = 1 To 1048576
      If Cells(Row, 1).Value = MaxVal Then
         Exit For
      End If
   Next Row
   MsgBox "Максимальное значение в строке " & Row
   Cells(Row, 1).Activate
End Sub


Sub NestedLoops()
   Dim MyArray(1 To 10, 1 To 10, 1 To 10)
   Dim i As Integer, j As Integer, k As Integer
   For i = 1 To 10
      For j = 1 To 10
         For k = 1 To 10
            MyArray(i, j, k) = -1
         Next k
      Next j
   Next i
End Sub


Back to Top