vba code sort multiple columns getting compile error!

RAM1972

Board Regular
Joined
Jun 29, 2014
Messages
217
Hi All

Tried a macro to sort column H,I,J to last data row, rows 1 are headers

the range are columns A to S to last data row

but getting a compile error

expected end with

Can anyone help

Code:
Sub testsort()


Dim LR As Long
With ActiveWorkbook.Worksheets("datatest")
    LR = .Range("A" & Rows.Count).End(xlUp).Row.Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=.Range("H1:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=.Range("I1:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=.Range("J1:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange .Range("A1:S" & LR)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


End Sub:confused:
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You need another End With on a line after the End With you already have.
 
Upvote 0
You need another
End With on a line after the End With you already have.

Add line end with as requested but get a yellow line

Run time error 424
object required

LR = .Range("A" & Rows.Count).End(xlUp).Row.Sort.SortFields.Clear

Any idea what is going wrong


 
Upvote 0
Row is Long type. It doesn't have Sort property.
Anyway, what you're trying to achieve with this line?
 
Upvote 0
Probably just clearing any previous sort fields and so should be on a separate line (must admit I can't remember a time when I needed to but the recorder always throws it in).

Code:
Sub testsort()


Dim LR As Long
With ActiveWorkbook.Worksheets("datatest")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=.Range("H1:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=.Range("I1:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=.Range("J1:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange .Range("A1:S" & LR)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With

End Sub
 
Last edited:
Upvote 0
Row is Long type. It doesn't have Sort property.
Anyway, what you're trying to achieve with this line?

As per code above.

I have data from A to S
Row 1 are headers
Data starts at rows 2
Sheet name is datatest
from Active workbook whole range from A to S but sort criteria column H , I and J.

Hope it helps
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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