Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long
Dim allDataEntered As Boolean
Dim i As Integer
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If Not Intersect(Target, ws.Columns("A:D")) Is Nothing Then
allDataEntered = True
For i = 1 To 4
If ws.Cells(Target.Row, i).Value = "" Then
allDataEntered = False
Exit For
End If
Next i
If allDataEntered Then
Set rng = ws.Range("A1:J" & lastRow)
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("A2:A" & lastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
With ws.Sort
.SortFields.Clear
.SortFields.Add(ws.Range("A2:A" & lastRow), _
xlSortOnCellColor, xlAscending, , _
xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Set rng = ws.Range("A1:J" & lastRow)
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("H2:H" & lastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End If
End If
If Not Intersect(Target, ws.Columns("E")) Is Nothing Then
If Target.Value <> "" Then
Set rng = ws.Range("A1:J" & lastRow)
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("A2:A" & lastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
With ws.Sort
.SortFields.Clear
.SortFields.Add(ws.Range("A2:A" & lastRow), _
xlSortOnCellColor, xlAscending, , _
xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Set rng = ws.Range("A1:J" & lastRow)
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("H2:H" & lastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End If
End If
End Sub