G.M
G.M

Reputation: 365

Use of .Range variable causes an runtime error in vba

hope you can help me with a little issue. I want to copy a range of my current workbook to another open workbook.

Doesnt sound like an issue but it already crashes within the copy line.

I already tried to save the current/active Worksheet/book in a varaible and execute it like that but the error still appears. I used the combination of range and cells a ew times already so i doubt that this is the issue.

Maybe some of you had a similar problem already? Or possible there is a better way of doing it?

Thanks alot in advance.

ThisWorkbook.Worksheets("Test").Range(Cells(8, 2), Cells(AmountofRows, AmountofColumns)).Copy

Upvotes: 0

Views: 273

Answers (1)

Tom
Tom

Reputation: 9878

I suspect it's due to you not being on the Active sheet when you run the command.

Try using this instead

With ThisWorkbook.Worksheets("Test")
    .Range(.Cells(8, 2), .Cells(AmountofRows, AmountofColumns)).Copy
End With

The way you have used Cells will just use the active sheet which will then fail when you try to copy with a mixed declaration. Make sure you always define your ranges fully

Upvotes: 3

Related Questions