Automatic sort of table into rank order when a new record is added

tbones

New Member
Joined
Jul 18, 2008
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hi all, I have been using a table that I input figures onto. What I want to be able to do is to have the rows move in order of rank automatically as new data is inputted.
So say I have 10 columns and say 10 rows and I want to add data to these rows I then want the table to sort in order of largest to smallest of say column 8 .

I have been using a VBA code that will do this if I double click on the header row however I dont want to have to do this and I want it to move the rows in order once the data has been inputted.

Here is the VBA I use so that I can click on the header to sort.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim KeyRange As Range
Dim ColumnCount As Integer
ColumnCount = Range("A1:FE25").Columns.Count
Cancel = False
If Target.Row = 1 And Target.Column <= ColumnCount Then
Cancel = True
Set KeyRange = Range(Target.Address)
Range("A1:FE25").Sort Key1:=KeyRange, Header:=xlYes
End If
End Sub

Any help to be able to do the same as the code but without having to double click on the header column to sort would be appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try the following
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("H:H"), Target) Is Nothing Then
    Application.EnableEvents = False
    With Range("A1").CurrentRegion
        .Sort Key1:=Range("H1"), order1:=xlDescending, Header:=xlYes
    End With
    Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Try the following
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("H:H"), Target) Is Nothing Then
    Application.EnableEvents = False
    With Range("A1").CurrentRegion
        .Sort Key1:=Range("H1"), order1:=xlDescending, Header:=xlYes
    End With
    Application.EnableEvents = True
    End If
End Sub
Thank you for this. I have tried this and put in the range I needed however it is coming back as an error highlighted below
1685935640883.png

Not sure why it it is not working. Would you have an idea at all please?
 
Upvote 0
Could you post a copy of your sheet using the XL2BB add in? Or alternatively share your file using Dropbox, Google Drive or similar file sharing platform?
 
Upvote 0
I suspect your dataset might not be contiguous, which means the .CurrentRegion method will not work. Try this instead - although you may need to enable events first (run the first code once only).
This once:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub

Then replace the code in post #2 with this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("FC:FC"), Target) Is Nothing Then
    Application.EnableEvents = False
    With Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, Cells(1, Columns.Count).End(xlToLeft).Column))
        .Sort Key1:=Range("FC1"), order1:=xlDescending, Header:=xlYes
    End With
    Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Try the following
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("H:H"), Target) Is Nothing Then
    Application.EnableEvents = False
    With Range("A1").CurrentRegion
        .Sort Key1:=Range("H1"), order1:=xlDescending, Header:=xlYes
    End With
    Application.EnableEvents = True
    End If
End Sub
I forgot to mention that I am using excel 365
 
Upvote 0
Thank you for providing the actual file - this changes things totally ;)
Because your column FC changes as a result of a formula, a Worksheet Change event will not work at all - the code needs to be triggered by a worksheet calculate event instead. The drawback is that the sheet will sort whenever a calculation occurs anywhere on the sheet - you can't specify which cells change via a calculation to trigger the code.
Here is the new code:
VBA Code:
Private Sub Worksheet_Calculate()
    Application.EnableEvents = False
    With Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, Cells(1, Columns.Count).End(xlToLeft).Column))
        .Sort Key1:=Range("FC1"), order1:=xlDescending, Header:=xlYes
    End With
    Application.EnableEvents = True
End Sub
and here is the link to your workbook with the code already added: football predictor project 4.xlsm
 
Upvote 0
Solution
Oh wow... that works as I wanted it to do so I can only but thank you for your help.
The way I will be populating the table is using different gameweek sheets which I will link to each player. What I do then is to manually input everyones predictions and then once the games have been played i then input the actual scores and it will populate the whole table based on the players predictions. So in essence it works perfectly. Thank you
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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