Problems with an Auto Sort code

JJRudd

New Member
Joined
Dec 6, 2005
Messages
34
Practice EPR Tracker.xls
ABCDEF
1Date Last SavedProj C/O Date Color Key:Overdue30 Days90 Days
212/16/05 13:51
3NameSupv NameSupv BeganDate beganLast ReportProj C/O Date
42-Jan-053-Jul-011-Jan-051-Jan-06
517-Mar-059-Apr-8711-Jan-0511-Jan-06
616-Jan-0527-May-0315-Jan-0515-Jan-06
71-Feb-053-Jun-0315-Jan-0515-Jan-06
89-Feb-0516-Oct-0119-Jan-0519-Jan-06
920-Jan-0528-Aug-0119-Jan-0519-Jan-06
1029-Jan-0528-May-0328-Jan-0528-Jan-06
113-Feb-053-Jun-032-Feb-052-Feb-06
1227-Jun-0513-Sep-0518-Feb-0518-Feb-06
132-Mar-056-May-941-Mar-051-Mar-06
147-Mar-0522-Aug-906-Mar-056-Mar-06
1513-Apr-0513-May-9812-Apr-0512-Apr-06
1615-Apr-0522-Sep-9914-Apr-0514-Apr-06
1717-May-0524-Sep-0215-Apr-0515-Apr-06
1816-Apr-0524-Sep-0215-Apr-0515-Apr-06
1916-May-0515-Oct-0215-Apr-0515-Apr-06
EPR Tracker


Okay I'm using a code to automatically sort my list using F4. The problem is any time I change any cell, it sorts. Can anyone modify the code I'm using to auto sort only when a cell in column E gets changed? Column F is dependant on E. Here's the code I'm using.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim RNG1 As Range

Set RNG1 = Range("A4:Z100")
RNG1.Select
Selection.Sort Key1:=Range("G4"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub

Any help would be greatly appreciated.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
When do you want the list to be sorted?

A change in which cell should trigger the code?
Code:
Private Sub Worksheet_Change(ByVal Target As Range) 
Dim RNG1 As Range 

If Target.Address<>"$F$4" Then Exit Sub
Set RNG1 = Range("A4:Z100") 

Rng1.Sort Key1:=Range("G4"), Order1:=xlAscending, Header:=xlGuess, _ 
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ 
DataOption1:=xlSortNormal 

End Sub
 
Upvote 0
Try this code.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim isect As Range
Set isect = Application.Intersect(ActiveCell, Range("E4:E100"))
If isect Is Nothing Then
Exit Sub
Else
Set RNG1 = Range("A4:Z100")

RNG1.Sort Key1:=Range("G4"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End If
End Sub

It will run when any cell in the Range E4:E100 is changed.
 
Upvote 0
The above code only auto sorts if a cell in column E becomes blank. I want it to sort if any cell in column E changes at all. Whether it becomes blank or the date changes.
If I can read it right, the section of the code that's causing it to only change when blank is this part:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim isect As Range
Set isect = Application.Intersect(ActiveCell, Range("E4:E100"))
If isect Is Nothing Then
Exit Sub
Else
Set RNG1 = Range("A4:Z100")

Does anyone know how to fix this?
 
Upvote 0
Hello JJRudd,
To limit your worksheet_change to execute on a change in column E only, all you need do is add one line at the top of the code. (ie.)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Then Exit Sub
Dim RNG1 As Range

Set RNG1 = Range("A4:Z100")
RNG1.Select
Selection.Sort Key1:=Range("G4"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub
That help?
 
Upvote 0
Well, kind of. Where do I make the change in the code you gave me to only change when colum E? Would it look like this?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <$E> Then Exit Sub
Dim RNG1 As Range

Set RNG1 = Range("A4:Z100")
RNG1.Select
Selection.Sort Key1:=Range("G4"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub
 
Upvote 0
Nope, you would just use it as is.
The column (in this case) is being referred to by its number.
For example:
Column A = 1
Column B = 2
(etc.)
So... the statement
If Target.Column <> 5 Then Exit Sub
translates to:
"If the change did not happen in the 5th column (Column E) Then do nothing.

That help make it any clearer?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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