Range throwing up an error in VBA

grabrail

Board Regular
Joined
Sep 6, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hi
I am working on some VBA code and have come across an error when trying to select a number of cells in a column to determine how many entries there are.

The code I am using is borrowed from another spreadsheet I have that I inherited, but is working no problem.

The Code

VBA Code:
Worksheets("DPU Report Template").Select
Worksheets("DPU Report Template").Range("A37").Select
Range(Selection, Selection.End(xlDown)).Select

When I run this I get the error:

Method Range of Object Worksheet Failed, with the 3rd line highlighted

One difference between this sheet and the one that is working is, on the one that is working, the button that is pressed to run this code is on the same worksheet as the selected area, on the one that is not working, the button is on a different worksheet
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
My guess is that the Range call is referencing the sheet the button is on and not the "DPU Report Template" sheet. Try adding the Worksheets("DPU Report Template"). reference to the front of that Range call.
 
Upvote 0
Solution
Your code worked for me.
A bit shorter though
VBA Code:
Sub MM1()
Worksheets("DPU Report Template").Select
Range(("A37"), Range("A37").End(xlDown)).Select
End Sub
 
Upvote 0
My guess is that the Range call is referencing the sheet the button is on and not the "DPU Report Template" sheet. Try adding the Worksheets("DPU Report Template"). reference to the front of that Range call.
Thanks, this did what I needed
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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