1

I'm using offset function in VBA. But, for example, when I'm in the first row, offset(-5,0) returns an error message. Of course, I can't offset -5 rows when I'm in first row.

How can I check it?

I tried using if rangeEx = range("A1") but it doesn't work.

4
  • Can you provide some more code? I'm guessing you probably have something like rangeEx.Offset rather that just offset(-5,0) as you have written in your question... If you provide that bit more, we could help... Commented Apr 2, 2014 at 16:15
  • 1
    try If rangeEx.Column>5 Then rangeEx.Offset(-5,0).Value="test" Commented Apr 2, 2014 at 16:16
  • 1
    @SiddharthRout, misstype:) If rangeEx.Row>5 Then .. Commented Apr 2, 2014 at 16:27
  • I use rangeEX.column to check, and it work. Thank you. ^^ Commented Apr 2, 2014 at 16:33

2 Answers 2

2

You can check the .Row property of the cell and see if it's less than or equal to 5.

Sub Test()

    Dim RangeEx As Range
    Set RangeEx = ActiveCell

    If RangeEx.Row <= 5 Then
        MsgBox "Invalid!"
    Else
        MsgBox "Offset at " & RangeEx.Offset(-5, 0).Address
    End If

End Sub

Result on rows 1-5:

enter image description here

Result on row 6++:

enter image description here

Let us know if this helps.

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

Comments

2

This example uses Selection

Sub RangeCheck()
If ActiveCell.Row < 6 Then
    MsgBox ".OFFSET(-5,0) will fail"
End If
End Sub

Comments

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.