ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Automatic Sorting (https://www.excelbanter.com/excel-discussion-misc-queries/135549-automatic-sorting.html)

Blade370

Automatic Sorting
 
I know this code automatically sorts a column in ascending order. But what
changes would you have to make to it to get it to sort a range of colums say
from A1 to F1 or maybe more?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim whereIam As Range
Set whereIam = ActiveCell
If Intersect(Target, Range("B:B")) Is Nothing Then
Exit Sub
End If
Range("B1:" & Range("B1").End(xlDown).Address).Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending
'and back to where you started
whereIam.Select
End Sub


Dave Peterson

Automatic Sorting
 
Based on column B?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim whereIam As Range
Dim RngToSort as range

Set whereIam = ActiveCell

If Intersect(Target, Range("B:B")) Is Nothing Then
Exit Sub
End If

with me
set rngtosort = .range("A1:F" & .cells(.rows.count,"B").end(xlup).row)
end with

with rngtosort
.cells.sort key1:=.columns(2), Order1:=xlAscending
end with
'and back to where you started
whereIam.Select
End Sub

Blade370 wrote:

I know this code automatically sorts a column in ascending order. But what
changes would you have to make to it to get it to sort a range of colums say
from A1 to F1 or maybe more?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim whereIam As Range
Set whereIam = ActiveCell
If Intersect(Target, Range("B:B")) Is Nothing Then
Exit Sub
End If
Range("B1:" & Range("B1").End(xlDown).Address).Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending
'and back to where you started
whereIam.Select
End Sub


--

Dave Peterson


All times are GMT +1. The time now is 02:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com