Select variable Range (by Cells function)

L

Legacy 146544

Guest
Hi all,

I need to select a range (on a different worksheet than the active one) based on input so I created this code:

ThisWorkbook.Worksheets("Debiteuren").Activate
Worksheets("Debiteuren").Range(Cells(RLocatie - 1, 1), Cells(RLocatie - 1, 52)).Select

Where RLocatie is the row number.

But I get runtime error 1004 on the second line of code. For some reason Excel can't select the range?

Does anyone know what might be the problem?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi

You can't select a range on a worksheet that isn't active.

Your code shouldn't fail, however, as you have activated the relevant worksheet first - is the code in a sheet module rather than in a standard module?
 
Upvote 0
And:

if I run the macro from the worksheet where the button (which triggers the macro) is located, the code works fine!

The button is on Sheet "wIntern"

This code:
Worksheets("Debiteuren").Range(Cells(RLocatie - 1, 1), Cells(RLocatie - 1, 52)).Select
Works perfectly!

What might be the case?
 
Upvote 0
When you have code in a sheet module, any not-fully-referenced calls to objects refer to the sheet in which the code appears so in your line:

Code:
Worksheets("Debiteuren").Range(Cells(RLocatie - 1, 1), Cells(RLocatie - 1, 52)).Select

You have a Range referenced to sheet "Debiteuren" but Cells referenced to sheet "wIntern" hence a mismatch. You can avoid this by fully referening:

Code:
With ThisWorkbook.Worksheets("Debiteuren")
  .Activate
  .Range(.Cells(RLocatie - 1, 1), .Cells(RLocatie - 1, 52)).Select
End With

Note the periods before the Cells property. (usual caveats of "you don't normally need to Select anything apply)
 
Upvote 0
Yes that is the problem!

And your code is the solution I was looking for.

I can go writing code now, Thank you Richard
 
Upvote 0
Richard,

is there a way to overcome the problem by code like: worksheets(sheetx).cells(2,1)...?

Instead of the with solution

That would be very handy for future excel work for me.

Thx in advance.
 
Upvote 0
Yep - it's more verbose and won't execute quite as quickly (not that you're ever likely to notice):

Code:
Worksheets("Debiteuren").Activate
Worksheets("Debiteuren").Range(Worksheets("Debiteuren").Cells(RLocatie - 1, 1), Worksheets("Debiteuren").Cells(RLocatie - 1, 52)).Select

Or you could use Resize:

Code:
Worksheets("Debiteuren").Cells(RLocatie - 1,1).Resize(,52).Select
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top