VBA sort variable range - Run-time error '1004' : Method 'Range' of object'_Global' failed

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I am running a macro that pulls in a grouping of files, and then before sending it to a chart, make sure that all of the data is in timestamp order.

At the top of the pull-in page, I've created cells that do a count of rows and columns (Cell "A1" is the last data row, Cell "A2" is the last column (in this case, DE). I've done this because, depending on the project, both of those counts vary by quite a bit.

In my macro soft portion, my range is not working. Help!

Code:
Dim LastRow As String '(I've also tried it as Long, but that made no difference)
Dim LastCol As String

Sheets("All Data").Select
    LastRow = Range("A1")
    LastCol = Range("A2")
    
    Range("A6").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    ActiveWorkbook.Worksheets("All Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("All Data").Sort.SortFields.Add Key:=Range( _ '(THIS IS THE-HILITED SECTION WITH THE RUN-TIME ERROR)
        "B6:B:" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("All Data").Sort.SortFields.Add Key:=Range( _
        "C6:C" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("All Data").Sort
        .SetRange Range("A6:" & LastCol & LastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

What am I doing wrong? Please help!

Thanks :)
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You have an extra :

Code:
"B6:B[COLOR=#ff0000]:[/COLOR]" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _

should be
Code:
"B6:B" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _

when you get the row or column it is returned as a number so it should be DIMed as long
 
Last edited:
Upvote 0
Eep! A pinger mistake. I am my own worst proofreader.

Thank you so much for your help!
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,034
Members
449,061
Latest member
TheRealJoaquin

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