Range Error in sort - Range of Object _Global failed

IREALLYambatman

Board Regular
Joined
Aug 31, 2016
Messages
63
So I used this code to find the last column letter...

Code:
With ActiveWorkbook.Worksheets("Edit Here")
    LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column
End With
ColumnLetter = Split(Cells(1, LastCol).Address, "$")(1)

And then when I tried to sort, using that in a range, I get an error.. Is Range("A11", ColumnLetter & 11) not a valid way to define a range? It works for the rest of my script..?
I get the error: Method: Range of Object _Global failed. Is that a bad way to call that range? Is there a better way I should be using?


Code:
    ActiveWorkbook.Worksheets("Edit Here").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Edit Here").Sort.SortFields.Add Key:=Range("A11", ColumnLetter & 11), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "salted,SampleID,CCV,ccv,CCB,ccb,MB-,mb-,LCS-,lcs-,LCSD-,lcsd-", DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Edit Here").Sort.SortFields.Add Key:=Range("A13", ColumnLetter & 13), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "salted,Dilution", DataOption:=xlSortNormal
'    With ActiveWorkbook.Worksheets("Edit Here").Sort
'        .SetRange Range("A1:", ColumnLetter & 1)
'        .Header = xlGuess
'        .MatchCase = False
'        .Orientation = xlLeftToRight
'        .SortMethod = xlPinYin
'        .Apply
'    End With
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I got it.. finally, I realized that it wasn't the column that was causing the error but the row count.. I should have included all the rows in the sort, so it worked when I used:

Code:
.SetRange Range("A11:" & ColumnLetter & LASTROW)
 
Upvote 0
Glad you sorted it thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,048
Members
449,206
Latest member
Healthydogs

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