Thanks for posting your solution. It is a good example declaring a variable in the module scope to be able to assign their values in some procedures and accessing them in other procedures. So, it would be great if you could also post the procedure that actually assigns variables i and j. Then it would be more helpful example to the future readers.
Also, the following declaration:
declares j as an Integer variable, however, i as a Variant. Same line declaration is correct and acceptable (although I personally don't prefer that for readability purposes). However, each variable should be declared with their type as shown below:
VBA Code:
Public i Integer, j As Integer
It won't affect the code since the Variant type is OK in this particular case, but you might be thinking that i is also declared as an Integer when it is used in this form. It is not.
While we are talking about variable types, just to make sure not causing an overflow error, declaring i as Long type would be better choice since row index can be more than 32767. Again, this will probably not affect most applications including yours, but I remember the days right after Excel 2007 (1M rows) that I found myself making this mistake a lot in large worksheets.