VBA To sort range

Corleone

Well-known Member
Joined
Feb 2, 2003
Messages
841
Office Version
  1. 365
I need to revise this so that it sorts the range should it increase or reduce presumably by changing the line half way down ".SetRange Range("G3:V62")" into something similar to the Range(Selection, Selection.End(xlDown)).Select command in the 2nd line
any help appreciated

ta -code below

--------------------------------
Sub Sort_by_1day()
'
' Macro1 Macro
'

'
Range("G3:AD3").Select

Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("PORT").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("PORT").Sort.SortFields.Add2 Key:=Range("m3:m3"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("PORT").Sort
.SetRange Range("G3:V62")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("G26").Select
End Sub
 

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
Try replacing that code with
VBA Code:
With Worksheets("PORT")
   .Range("G3:AD" & .Range("G" & Rows.Count).End(xlUp).Row).Sort key1:=.Range("M3"), order1:=xlDescending, Header:=xlYes
End With
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,768
Members
449,122
Latest member
sampak88

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