Assigning Range gives Run-time error '91'

dhunton

New Member
Joined
Apr 14, 2016
Messages
48
The line which sets MyRange gives the error with the detail of "Object variable or With block variable not set." I'm hoping that there is simply a typo or something easy I'm missing.

And any tips on a better way to do this sort (or if it will even work once I get past the error LOL) would be appreciated. The 3rd column (the one being sorted on) is a datetime format. The SourceFile1 is exported from another program.

Code:
Option Explicit
    Const SourceSheet As String = "Page 1"
    Const S1SC As Integer = 3
    Const Source1FirstLine As Integer = 2
    Const Source1LastColumn As Integer = 13
 
 
Sub CallsReport()
 
    Dim SourceBook1 As String
    Dim MyRange As Range
    Dim SortRange As Range
 
 
    SourceBook1 = “FileName.xlsx”
    Source1LastLine = Workbooks(SourceBook1).Sheets(SourceSheet).Range("A1").End(xlDown).Row
 
    'Sort Source1
    MyRange = Range(Workbooks(SourceBook1).Sheets(SourceSheet).Cells(Source1FirstLine, 1), _
Workbooks(SourceBook1).Sheets(SourceSheet).Cells(Source1LastLine, Source1LastColumn))
    SortRange = Range(Workbooks(SourceBook1).Sheets(SourceSheet).Cells(Source1FirstLine, S1SC), _
Workbooks(SourceBook1).Sheets(SourceSheet).Cells(Source1FirstLine, S1SC))
    MyRange.Sort key1:=Range(SortRange), order1:=xlDescending
 
End Sub 'Calls Report
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try adding the keyword "Set" before "MyRange" just before you assign it.

Code:
Set MyRange = ....

Caleeco
 
Last edited:
Upvote 0
OK, did that for MyRange and SortRange, and now I get an error on the MyRange.Sort line.

Run-time error '1004':
Method 'Range' of object '_Global' failed

:(
 
Upvote 0
OK, did that for MyRange and SortRange, and now I get an error on the MyRange.Sort line.

Run-time error '1004':
Method 'Range' of object '_Global' failed

:(
I haven't tested your code but SortRange already is a range, so you shouldn't need the red text
Rich (BB code):
    MyRange.Sort key1:=Range(SortRange), order1:=xlDescending
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,263
Members
449,307
Latest member
Andile

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