0

So i've read a ton of posts on stack about this and for some reason I can't understand what my problem is. I'm new to excel VBA so it's possible it's very obvious so I apologize. Trying to create a range object using a variable column and row_number. (this isn't the actual project, but it's a lot less convoluted to create a simple example than my real project.) I get a Run-time error '424' Object required when I try and compile. Any ideas?

Sub test()
    Dim testObj As Range
    row_number = 3
    dataCol = "O"
    Set testObj = (dataCol & row_number)


End Sub

2 Answers 2

2

Below is how you can get the reference to the range:

Sub test()
    Dim testObj As Range

    row_number = 3
    dataCol = "O"

    Set testObj = ActiveSheet.Range(dataCol & row_number)


End Sub

This will create the reference to the cell O3 in the currently active worksheet.

If you need the reference to the range from other worksheet you can do it like that:

Set testObj = Worksheets("sheet_name").Range(dataCol & row_number)
Sign up to request clarification or add additional context in comments.

1 Comment

Figured it out. Thank you Gents! Appreciated!
2

You cannot simply construct a string and expect it to be a valid Range object. However, you can use that string to create one.

Set testObj = Range(dataCol & row_number)

This could equate to the INDIRECT function on a worksheet turing a text string into a valid cell reference.

1 Comment

Don't skip references, they're useful and make your posts outstanding.

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.