Home > OS >  Excel vba Offset issue
Excel vba Offset issue

Time:09-22

After running the following piece of code I would expect the cell to be D4 but it's E5. Why? Am I missing something?

***Sub off()
Dim testRange As Range
Set testRange = Range("b2:i10")
MsgBox ("Here: " & testRange.Range("B2").Offset(2, 2))
End Sub***

CodePudding user response:

testRange.Range("B2") refers to the second row and second column of the testRange range, not of the containing worksheet. In this case, that would be C3. You then offset two columns and two rows from there, which is E5.

CodePudding user response:

You have set testRange equal to range b2:i10. Thus, testRange.Range("B2") is the second column and second row in your test range B2:I10, or C3. If you offset C3 2 rows and 2 col, you land on E5. If the code is changed to {Range("B2").Offset(2, 2).Activate}, then D5 will be activated. I am not sure what the rest of your code is intended for, but this should answer your question I hope!

  • Related