0
    For j = 0 To 20
        For i = 0 To 20

            ReDim atoms(0 To 20, 0 To 20)
            ReDim atomschange(0 To 20, 0 To 20)
            atomschange(j, i) = 0

            If i Mod 2 = 0 And j Mod 2 = 0 Then
                [B2].Offset(j, i).Interior.ColorIndex = 37
                atoms(j, i) = 1
            Else
                [B2].Offset(j, i).Interior.ColorIndex = 36
                atoms(j, i) = 0
            End If
        Next i
    Next j
End Sub

what am i doing wrong?

5
  • 2
    Please format your code properly. What error are you getting? What line is erroring? What have you tried? Commented Jul 30, 2018 at 14:31
  • Expected array,compile error that's the error i have tried changing my variables to long,integer, nothing worked dk why Commented Jul 30, 2018 at 14:33
  • Should the 3 lines starting REDIM to atomschange be outside the two loops? Commented Jul 30, 2018 at 14:39
  • When you get the error, hit the Debug and share which line is highlighted. That is the line to focus on. Commented Jul 30, 2018 at 14:39
  • he would just highlight the name of my array Commented Jul 30, 2018 at 14:45

1 Answer 1

7
  1. You are redim'ing your arrays inside a loop which reinitializes, then dropping any data inside the array. Use the PRESERVE keyword to insure you don't dump your array when redimensioning it ReDim Preserve atoms(0 To 20, 0 To 20)

  2. Redimensioning your arrays to the exact same size makes no sense. They are already 0 To 20, 0 To 20 because you already set that. Redim them once outside of your for loops and then leave them alone.

  3. Insure that your atoms and atomschange arrays are declared properly at the top of your code. It's complaining (probably when you redim them) that those two variables aren't arrays.

You know the size of the arrays before this code executes, so declare them and dim them properly in the declaration, then just use them (removing the superfluous and incorrect use of redim in your loop:

Sub somesumroutine()
    Dim atoms(0 to 20, 0 to 20) as Integer
    Dim atomschange(0 to 20, 0 to 20) as Integer

    For j = 0 To 20
        For i = 0 To 20             
            atomschange(j, i) = 0

            If i Mod 2 = 0 And j Mod 2 = 0 Then
                [B2].Offset(j, i).Interior.ColorIndex = 37
                atoms(j, i) = 1
            Else
                [B2].Offset(j, i).Interior.ColorIndex = 36
                atoms(j, i) = 0
            End If
        Next i
    Next j
End Sub

Consider using UBound and LBound of your atoms array to set the loops so you don't have to use the 0 to 20 over and over again. This way you can dim your arrays once and leave the rest of your code alone:

Sub somesumroutine()
    Dim atoms(0 to 20, 0 to 20) as Integer
    Dim atomschange(0 to 20, 0 to 20) as Integer

    For j = lBound(atoms, 1) To uBound(atoms, 1)
        For i = lBound(atoms, 2) To uBound(atoms, 2)            
            atomschange(j, i) = 0

            If i Mod 2 = 0 And j Mod 2 = 0 Then
                [B2].Offset(j, i).Interior.ColorIndex = 37
                atoms(j, i) = 1
            Else
                [B2].Offset(j, i).Interior.ColorIndex = 36
                atoms(j, i) = 0
            End If
        Next i
    Next j
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, so I was right about two of the lines - must be improving... :) plus one from me.

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.