• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

The same code behaves differently in different workbooks

tomas

Active Member
Hello ,

I am new to VBA and use a tutorial to learn. In attached workbook at the bottom of module 2 you find subprocedure "loops_exercise". The code should put something like chessboard pattern in active sheet starting at active cell which is set by offset_row variables.... But when I run the code the pattern starts from cell A1. But if I put the same code into new workbook it worked as expected. Can someone explain what is going on ?
 

Attachments

  • vbbbbbbbba.xlsm
    23.6 KB · Views: 2
Hi ,

Your code is not making use of the variables offset_row and offset_col.

You need to change your For ... Next loops as follows :

For r = offset_row To offset_row + NB_CELLS

For c = offset_col To offset_col + NB_CELLS

Narayan
 
Thanks Narayan

But you know why in this workbook it works without doing your proposed change
 

Attachments

  • here it works.xlsm
    13.8 KB · Views: 2
Hi ,

Because your code here is doing the addition within the loop in these 2 statements :

Cells(r + offset_row, c + offset_col).Interior.Color = RGB(200, 0, 0) 'Red

Cells(r + offset_row, c + offset_col).Interior.Color = RGB(0, 0, 0) 'Black

Narayan
 
Back
Top