Looping TOIL List

ESC1989

New Member
Joined
Feb 20, 2025
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi there, I'm trying to create a table that keeps track of which members of my team have priority when it comes to getting Time off in Lieu (TOIL). I need a fairly simple table with several columns

Priority number eg. 1st, 2nd, 3rd, 4th, etc
Staff name
Date TOIL Last Given

When the staff members name is clicked (or a checkbox), it should move their row to the bottom of the list and update the date. The next staff member will be moved up the list so if they were '2nd', they will now be '1st'

Please could anyone help?
 
Hi there, I'm trying to create a table that keeps track of which members of my team have priority when it comes to getting Time off in Lieu (TOIL). I need a fairly simple table with several columns

Priority number eg. 1st, 2nd, 3rd, 4th, etc
Staff name
Date TOIL Last Given

When the staff members name is clicked (or a checkbox), it should move their row to the bottom of the list and update the date. The next staff member will be moved up the list so if they were '2nd', they will now be '1st'

Please could anyone help?
Set up a table as below named 'tblTOIL' in a new worksheet and copy the VBA code into the worksheet code module.

If you change the date for any person then the table will be sorted and the person who has waited the longest for TOIL wll rise to the top.

If dates are the same then the priority will be the same.

Version 4 Average.xlsm
ABCD
1PriorityNameDate
21stName 725/08/2024
32ndName 1226/08/2024
43rdName 529/08/2024
54thName 630/08/2024
65thName 931/08/2024
76thName 201/09/2024
86thName 301/09/2024
97thName 120/09/2024
107thName 820/09/2024
118thName 1128/09/2024
128thName 1028/09/2024
139thName 429/09/2024
14
TOIL1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C2:C13Cell Value=MIN($C$2:$C$13)textNO


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim intPriority As Integer

  If Target.CountLarge > 1 Then
    Exit Sub
  End If

  If Target.Column = 3 Then
  
    If Not IsDate(Target.Value) Then
      MsgBox "Please enter a valid date.", vbOKOnly, "Warning!"
      Application.EnableEvents = False
      Target.Value = ""
      Application.EnableEvents = True
      Target.Select
      Exit Sub
    End If
   
    Call subSortData
    
  End If
   
  Application.EnableEvents = False
  intPriority = 1
  Cells(2, 1).Value = fncOrdinalIndicator(intPriority)
  For i = 3 To ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row
    If Cells(i - 1, 3).Value < Cells(i, 3) Then
      intPriority = intPriority + 1
    End If
    Cells(i, 1).Value = fncOrdinalIndicator(intPriority)
  Next i
  Application.EnableEvents = True
  
End Sub

Private Sub subSortData()
Dim loToil As ListObject
  
  Set loToil = ActiveSheet.ListObjects("tblTOIL")

  loToil.Sort.SortFields.Clear
  loToil.Sort.SortFields.Add2 Key:=Range("tblTOIL[Date]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
  With loToil.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
  
End Sub

Private Function fncOrdinalIndicator(ByVal Number As Long) As String
Dim strOrdinalIndicator As String

  Select Case True
    Case Number Mod 100 = 11, Number Mod 100 = 12, _
      Number Mod 100 = 13
      strOrdinalIndicator = "th"
    Case Number Mod 10 = 1
      strOrdinalIndicator = "st"
    Case Number Mod 10 = 2
      strOrdinalIndicator = "nd"
    Case Number Mod 10 = 3
      strOrdinalIndicator = "rd"
    Case Else
      strOrdinalIndicator = "th"
  End Select
 
  fncOrdinalIndicator = Number & strOrdinalIndicator
 
End Function
 
Upvote 0
Another option

You could try this 'event' code. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test by double-clicking a name in column B.
4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  If Target.Column = 2 And Target.Row > 1 And Len(Target.Value) > 1 Then
    Application.ScreenUpdating = False
    Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(, 2).Value = Array(Target.Value, Date)
    Target.Resize(, 2).Delete Shift:=xlUp
    Application.ScreenUpdating = True
  End If
End Sub

Here is my original data

ESC1989.xlsm
ABC
1PriorityNameLast date
21stJen22/02/2024
32ndTom7/07/2024
43rdAnn29/07/2024
54thKen15/08/2024
65thJoe23/09/2024
76thKim28/09/2024
87thJan12/11/2024
98thAbe27/11/2024
109thBev21/01/2025
1110thDom11/02/2025
12
Sheet1


This is what I get after I double-clicked Ken

ESC1989.xlsm
ABC
1PriorityNameLast date
21stJen22/02/2024
32ndTom7/07/2024
43rdAnn29/07/2024
54thJoe23/09/2024
65thKim28/09/2024
76thJan12/11/2024
87thAbe27/11/2024
98thBev21/01/2025
109thDom11/02/2025
1110thKen22/02/2025
12
Sheet1
 
Upvote 0

Forum statistics

Threads
1,226,837
Messages
6,193,253
Members
453,784
Latest member
Chandni

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