0

I'm learning Excel VBA Programming for Dummies, 3rd Edition. There's an example which uses nested For-Next loops to initialize a three-dimensional array with the value of 100.

I want to see this sub procedure's result.

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) = 100
            Next k
        Next j
    Next i
End Sub

I tried using a MsgBox to display MyArray, but it says Type mismatch. Is there a way to show the array?

0

1 Answer 1

3

Ok, a couple of things.

  1. Excel usually shows 2D data (row & col are the same as height and width or X and Y on a graph). You have 3D data.

  2. If you insist on forcing Excel to show the data, then you would need to use worksheets as the 3rd dimension. So you would create a sheet for each I and on that sheet, you would fill in each cell with j and k (where j was a row and k was a column). If you do this right, you should end up with 10 sheets each with 100 in every cell from a1 to j10.

As it appears you are taking a class, I will leave it to you to figure out how to write the data out to the sheets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.