Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 197

NOTE

Оглавление

The ExitForDemo procedure is presented to demonstrate how to exit from a For-Next loop. However, it's not the most efficient way to activate the largest value in a range. In fact, a single statement does the job.

Range("A:A").Find(Application.WorksheetFunction.Max _ (Range("A:A"))).Activate

The previous examples use relatively simple loops. But you can have any number of statements in the loop, and you can even nest For-Next loops inside other For-Next loops. Here's an example that uses nested For-Next loops to initialize a 10 × 10 × 10 array with the value –1. When the procedure is finished, each of the 1,000 elements in MyArray contains –1.

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 ' [More code goes here] End Sub

Excel 2019 Power Programming with VBA

Подняться наверх