0

I'm getting the error mentioned here:

Error

…From this section of code...

Code1

...and as you'll see, the cell variables for the lastrow and lastcol are reporting fine, so why am I getting this error?

code2

code3

I tried adjust it as so too, but still the same issue.

code4

Basically, I'm just trying to set Rg as a range using cell values as determined by the two variables above. Using Excel 2016 VBA. Thanks. Will check back tomorrow.

1
  • 1
    To set an object, you need to use the Set keyword Commented Mar 13, 2020 at 1:23

2 Answers 2

2

Range is an Object so you need to use the Set command rather than just '=' which you can use for standard variables such as Sting, Integer etc. So in your example:

Set Rg = Range(Cells(1, 1), Cells(lastRow, LastCol))
Sign up to request clarification or add additional context in comments.

Comments

1

Please find below how to set ranges:

Sub test()

    Dim rng_Single As Range, rng_Union As Range

    'Refer to the sheet you want to work with
    With ThisWorkbook.Worksheets("Sheet1")

        'Use the "." before range or cell in order to refer to the worksheet mention in the with statement

        'Set the range.
        Set rng_Single = .Range(.Cells(1, 1), .Cells(1, 5))
        'Merge two ranges of the same worksheet
        Set rng_Union = Union(.Range(.Cells(3, 1), .Cells(3, 5)), .Range(.Cells(4, 1), .Cells(4, 5)))

    End With

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.