Please help me sort in VBA and help me kill my errors

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
Found some code a Contextures for locating the LAstRow and LastColumn in a worksheet. Trying to combine it with a macro to sort my data. I am trying to modify the macro-recorder's data to allow for the variable end of a worksheet (I recorded it such that it sorted from A1:Q205).

I come up with two errors here. First, syntax error at any reference to LastRow - which I seem to have defined as the address of the last CELL... though I don't know how, reading the code.

Second, it doesn't actually sort, even when I can get the code to run. I want it to sort by column K first, then column E (Timezone and State, respectively).

Help please? Here's my code:
Code:
Sub Formatter()
Application.ScreenUpdating = FalseApplication.Calculation = xlCalculationManual
Dim LastColumn As Integer
Dim LastRow As Long
Dim LastCell As Range
    If WorksheetFunction.CountA(Cells) > 0 Then
        'Search for any entry, by searching backwards by Rows.
        LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        'Search for any entry, by searching backwards by Columns.
        LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
                'MsgBox Cells(LastRow, LastColumn).Address
     '   LastCell = Cells.Address(LastRow, LastColumn)
        
    End If


    Cells.Select
    Selection.ClearFormats
    Columns("O:O").Select
    Selection.NumberFormat = "m/d/yyyy"
    Cells.Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("K2:K" & LastRow & ") _ '/////syntax error
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add Key:=Range("E2:E" & LastRow & ") _ '/////syntax error
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort
        .SetRange ("A1:Q" & LastRow)'///syntax error
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
       .Apply
    End With
'///other stuff
End Sub

Here's some sample data that is SHOULD be sorting:
Code:
PHONE,NAME,ADDRESS,CITY,STATE,ZIP,SERIAL,TYPE,ID1,ID2,TIMEZONE,OTHER
5551112222,JAmes,300 Any St,Culver, GA,30016,2344,A,3423,3434,Eastern,gt43
8881112223,Amy,344 A Ave,Anthem,AZ,85086,4813,B,e34,345,Mtn,345
7775465656,Robert,72 Main ST,Anytown,MD,21102,3532,A,5463,Eastern,46j2
8002223221,Janet,900 Oak St,Laramie,WY,82070,843as,B,3453,5642,Mtn,342jh
5552143251,Jeannie,6464 Manchester RD,Daphne,AL,36526,A,343534,746t,Central,43535s
6541234561,David,4 B st,Charlottesville,VA,22901,B,3424,4543,Eastern,345jh
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
ok some changes:

Code:
    ActiveSheet.Sort.SortFields.Add Key:=Range("K2:K" & LastRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add Key:=Range("E2:E" & LastRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange ("A1:Q" & LastRow)
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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