0

Hi could you please advise why the below code is not working? I would like to fill in the cells with "aaaa". I am VBA beginner and would appreciate if you provide some theorethical reference. Thank you

Sub aa()
   Dim row As Integer
   Dim col As Integer

   For row = 1 To 20
       For col = 1 To 20
           Cells(row, col) = "aaaa"

       Next row
   Next col

End Sub
1
  • 2
    Your Next row and Next col are mixed. And btw how exactly code is not working? Commented Jun 28, 2018 at 13:27

1 Answer 1

1

Hi it is important for you to learn the Pragmatics of VBA.

Always remember that when you are working with a nested for loop, make sure that you are calling the right "next" instance for that loop.

In your code, the "next row" should be on the outer for loop, and the "next col" should be in the inner loop.

Sub aa()
   Dim row As Integer
   Dim col As Integer

   For row = 1 To 20
       For col = 1 To 20
           Cells(row, col) = "aaaa"

       Next col
   Next row

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

2 Comments

Further, you do not need to actually specify the variable in next statements, and omitting them is often a good step when you encounter a for loop error
@TaylorScott While you do not need them, writing them may be best for readability and maintenance.

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.