K_McCormic
K_McCormic

Reputation: 334

For Loops in VB

I have a 5 by 5 matrix I want to populate and I would like to simplify this into for loops.

As I understand, I would need 2 for loops to complete this task?

I am still very new to VB hope you could understand

    Dim x(4, 4) As Char

    x(0, 0) = Mid(key, 1, 1)
    x(0, 1) = Mid(key, 2, 1)
    x(0, 2) = Mid(key, 3, 1)
    x(0, 3) = Mid(key, 4, 1)
    x(0, 4) = Mid(key, 5, 1)
    x(1, 0) = Mid(key, 6, 1)
    x(1, 1) = Mid(key, 7, 1)
    ....
    x(4, 4) = Mid(key, 25, 1)

Upvotes: 2

Views: 506

Answers (1)

Mark Hall
Mark Hall

Reputation: 54562

Try something like this:

Dim x As Integer
Dim y As Integer
Dim myMatrix(4, 4) As Char

For x = 0 To 4
    For y = 0 To 4
        myMatrix(x, y) =  Mid(key, (x * 5) + y + 1, 1)
    Next
Next

Upvotes: 7

Related Questions