Run=time error '1004': Method 'Range of Object'_Global' failed

2077delta

Active Member
Joined
Feb 17, 2002
Messages
252
Office Version
  1. 365
Platform
  1. Windows
I'm getting the runtime error whent I try to select a range using a defined name and not the actual range reference. I name my variables, then use the set command to define the range of the name(s). When the code selects the range using cell references, everything works . When I select the range using the name I defined, I get the error. It's very frustrating. My code is below. Also, if there is a more efficient way to write the code, please feel free to edit. Thank you.

Sub CVCB_dnld2()
'
Dim myRows As Integer
Dim myRange As Range
Dim myDrCr As Range
Dim myOrder As Range

' Format and Sort CVCB Activity Dowload

myRows = Worksheets("Sheet1").Range(Range("a2"), Range("a2").End(xlDown)).Rows.Count
Set myRange = Worksheets("Sheet1").Range(Range("A2"), Range("A2").Offset(myRows - 1, 10))
Set myDrCr = Worksheets("Sheet1").Range(Range("E2"), Range("E2").Offset(myRows - 1, 1))
Set myOrder = Worksheets("Sheet1").Range(Range("K2"), Range("K2").Offset(myRows - 1, 0))

Worksheets("Sheet1").Range(Range("a2"), Range("a2").End(xlDown)).Select 'Works as intended
Worksheets("Sheet1").Range(Range("E2"), Range("E2").Offset(myRows - 1, 1)).Select 'Works as intended
Worksheets("Sheet1").Range(Range("K2"), Range("K2").Offset(myRows - 1, 0)).Select 'Works as intended

Range(myRange).Select 'Gets runtime error
Range(myDrCr).Select 'Gets runtime error
Range(myOrder).Select 'Gets runtime error
 

Attachments

  • 1700159689046.png
    1700159689046.png
    2.9 KB · Views: 6

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Change this

VBA Code:
Range(myRange).Select [B]'Gets runtime error[/B]
Range(myDrCr).Select [B]'Gets runtime error[/B]
Range(myOrder).Select [B]'Gets runtime error[/B]


To this:
VBA Code:
  myRange.Select
  myDrCr.Select
  myOrder.Select
 
Upvote 0
myRange (as well ast the other 2 variables) are already "Ranges"; so try
VBA Code:
myRange.Select
But this assumes that Sheet1 is the selected sheet, or you will still get a run time error.
And ...are you sure you need to "select" those ranges? (in most of the cases you may work on a range without selecting it)

EDIT: As already recommanded by DM :)
 
Upvote 0
Also consider that if you are on another sheet than Sheet1, then you must reference the sheet in all range objects:

For example:

Rich (BB code):
Sub CVCB_dnld2()
'
  Dim myRows As Integer
  Dim myRange As Range
  Dim myDrCr As Range
  Dim myOrder As Range
  
  ' Format and Sort CVCB Activity Dowload
  
  myRows = Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("a2"), Worksheets("Sheet1").Range("a2").End(xlDown)).Rows.Count
  Set myRange = Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("A2"), Worksheets("Sheet1").Range("A2").Offset(myRows - 1, 10))
  Set myDrCr = Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("E2"), Worksheets("Sheet1").Range("E2").Offset(myRows - 1, 1))
  Set myOrder = Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("K2"), Worksheets("Sheet1").Range("K2").Offset(myRows - 1, 0))
  
  Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("a2"), Worksheets("Sheet1").Range("a2").End(xlDown)).Select 'Works as intended
  Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("E2"), Worksheets("Sheet1").Range("E2").Offset(myRows - 1, 1)).Select 'Works as intended
  Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("K2"), Worksheets("Sheet1").Range("K2").Offset(myRows - 1, 0)).Select 'Works as intended
  
  myRange.Select
  myDrCr.Select
  myOrder.Select

End Sub

Or in an abbreviated way.
Note how to select a range, you must first be on the sheet where the object to be selected is located.

VBA Code:
Sub CVCB_dnld3()
'
  Dim myRows As Integer
  Dim myRange As Range
  Dim myDrCr As Range
  Dim myOrder As Range
  
  ' Format and Sort CVCB Activity Dowload
  With Sheets("Sheet1")
    myRows = .Range(.Range("a2"), .Range("a2").End(xlDown)).Rows.Count
    Set myRange = .Range(.Range("A2"), .Range("A2").Offset(myRows - 1, 10))
    Set myDrCr = .Range(.Range("E2"), .Range("E2").Offset(myRows - 1, 1))
    Set myOrder = .Range(.Range("K2"), .Range("K2").Offset(myRows - 1, 0))
    
    .Select
    
    .Range(.Range("a2"), .Range("a2").End(xlDown)).Select 'Works as intended
    .Range(.Range("E2"), .Range("E2").Offset(myRows - 1, 1)).Select 'Works as intended
    .Range(.Range("K2"), .Range("K2").Offset(myRows - 1, 0)).Select 'Works as intended
    
    myRange.Select
    myDrCr.Select
    myOrder.Select
  End With
  
End Sub


Regards
Dante Amor
:giggle:
 
Upvote 0
Change this

VBA Code:
Range(myRange).Select [B]'Gets runtime error[/B]
Range(myDrCr).Select [B]'Gets runtime error[/B]
Range(myOrder).Select [B]'Gets runtime error[/B]


To this:
VBA Code:
  myRange.Select
  myDrCr.Select
  myOrder.Select
Thank you, that worked. I was only selecting those ranges so I could see it was picking up the correct ranges. I usually try to avoid using Select when writing the actual code.
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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