Automatic Sorting

G

Guest

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
 
D

Dave Peterson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top