Sort query

CookieMonster76

Board Regular
Joined
Apr 30, 2015
Messages
195
Hi

I have the following which works

ActiveWorkbook.Worksheets("Analysis").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Analysis").Sort.SortFields.Add Key:=Range("H2:H" & Range("A" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Analysis").Sort
.SetRange Range("A1:I1404")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

The range which it needs to sort is not necessarily always rows 1 to 1404, so I have replaced the red text with

.SetRange Range("A1:I" & Range("A" & Rows.Count).End(xlUp).Row)

but this doesn't work.

Could someone correct me please?

Thanks

Paul
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
maybe use a helper cell that is =counta(A1:A30000)

so in your example that will be 1404 so maybe
.SetRange Range("A1:I & "cells(1,26)"")

assumes helper is Z1
 
Upvote 0
Hello, this method worked nicely for me:

Code:
Public Sub SortWorksheet()
  Dim rngSortRange As Excel.Range
  Dim rngSortField As Excel.Range
  
  On Error GoTo ErrHandler
  With ThisWorkbook.Worksheets("Analysis")
    Set rngSortRange = .Range("A1").CurrentRegion
    Set rngSortField = Intersect(rngSortRange, .Range("H:H"))
    
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=rngSortField, _
                         SortOn:=xlSortOnValues, _
                         Order:=xlAscending
    
    .Sort.SetRange rngSortRange
    .Sort.Header = xlYes
    .Sort.MatchCase = False
    .Sort.Orientation = xlByRows
    .Sort.Apply
  End With
  
ExitProc:
  Set rngSortRange = Nothing
  Set rngSortField = Nothing
  Exit Sub
  
ErrHandler:
  MsgBox Err.Description, vbExclamation, "Sort Worksheet"
  Resume ExitProc
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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