Auto renumber a queue

wreckerp

New Member
Joined
Oct 30, 2019
Messages
6
Hi all,

I have a list of items with a priority order with 1 being the highest priority. Sometimes, the priority will change and 1 will become 3.

Is there a way to auto renumber the cells so that when a priority changes (for example: #1 becomes #3 ), the list will automatically shift and readjust the priorities so that when I manually change #1 to #3 , #2 becomes 1 and #3 becomes 2 ?

I've seen an older thread with a similar problem but there wasn't any solutions.

Thanks in advance for the help.

Order/priorityItemsPrice
1A100
2B50
3C35
4D30
5E65
6F10

<tbody>
</tbody>
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the forum
It is possible to do what you want with VBA

Are there only 3 columns of data ?
Is each order/priority unique ?
(in other words - is every value in column A different to every other value in column A ?)
Is the data always sorted by priority ?
 
Upvote 0
This assumes 3 columns starting with column A, headers in row1
- range to match your data

Excel 2016 (Windows) 32 bit
A
B
C
D
1
Order/priorityItemsPrice
2
1​
A
100​
3
2​
B
200​
4
3​
C
300​
5
4​
D
550​
6
5​
E
70​
7
6​
F
20​
8
Sheet: BEFORE

Excel 2016 (Windows) 32 bit
A
B
C
D
1
Order/priorityItemsPrice
2
1​
C
300​
3
2​
A
100​
4
3​
B
200​
5
4​
D
550​
6
5​
E
70​
7
6​
F
20​
8
Sheet: AFTER

Code goes in SHEET module (will not work in Module1,Module2 etc)
right-click on sheet tab \ View Code \ paste code into that window \ back to Excel with {ALT}{F11}
save workbook as macro enabled

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myRange As Range, newP As Double
    If Target.Count > 1 Then Exit Sub
    If Not IsNumeric(Target) Then Exit Sub
    Set myRange = Range("A2", Range("A" & Rows.Count).End(xlUp))
    If Not Intersect(Target, myRange) Is Nothing Then
        Application.EnableEvents = False
        newP = Target
        Target = WorksheetFunction.Max(myRange) + 1
        Call SortData(myRange)
        myRange(myRange.Rows.Count, 1) = newP - 0.5
        Call SortData(myRange)
        Application.EnableEvents = True
    End If
End Sub

Private Sub SortData(aRange As Range)
    Dim cel As Range
    Application.ScreenUpdating = False
    aRange.Resize(, 3).Sort aRange(1, 1), xlAscending
    For Each cel In aRange
        cel = cel.Row - 1
    Next
End Sub
 
Upvote 0
An alternative method

This assumes 3 columns starting with column A, headers in row1
Code goes in SHEET module (will not work in Module1,Module2 etc)
right-click on sheet tab \ View Code \ paste code into that window \ back to Excel with {ALT}{F11}
save workbook as macro enabled


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range: Set Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
    Dim oldP As Long, newP As Double, Cel As Range
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Rng) Is Nothing Then
        On Error Resume Next
        Application.EnableEvents = False
            newP = Target
            Application.Undo
            oldP = Target
            Select Case newP
                Case Is < oldP: newP = newP - 0.5
                Case Is > oldP: newP = newP + 0.5
            End Select
            Target = newP
            Rng.Resize(, 3).Sort Rng(1, 1), xlAscending
            For Each Cel In Rng
                Cel = Cel.Row - 1
            Next
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Welcome to the forum
It is possible to do what you want with VBA

Are there only 3 columns of data ?
Is each order/priority unique ?
(in other words - is every value in column A different to every other value in column A ?)
Is the data always sorted by priority ?

Hello Yongle, apologies for the tardy response .

To answer your questions:
1. No, I have more than 3 columns, potentially over 30.
2. Yes, each position is unique, I don't want two items to share the same position value
3. Not always, but 95% of the time it will be so if that is an issue or a constraint of the code I have no problem sorting only by priority.

I've tried the first code you've provided and it seems to do what I am looking for.

My only question is concerning the amounts of columns: does it have to be specified in the code, or can the code work indifferently from the number of column so that if I had more over time (which could happen) it would still function properly?

Not sure if I can do it here but I'd be more than happy to provide my spreadsheet and give you more context if required.

Thank you so very much for your help! :biggrin:
 
Upvote 0
Hi Yongle,

I've adjusted the first code to take into account the following parameters and it seems to work pretty well:
- Data starts on row 13
- There are 30 columns in the spreadsheet

Here is the adjusted code. Thanks for your help!


Code:
Private Sub Worksheet_Change(ByVal Target As Range)    Dim myRange As Range, newP As Double
    If Target.Count > 1 Then Exit Sub
    If Not IsNumeric(Target) Then Exit Sub
    Set myRange = Range("A13", Range("A" & Rows.Count).End(xlUp))
    If Not Intersect(Target, myRange) Is Nothing Then
        Application.EnableEvents = False
        newP = Target
        Target = WorksheetFunction.Max(myRange) + 1
        Call SortData(myRange)
        myRange(myRange.Rows.Count, 1) = newP - 0.5
        Call SortData(myRange)
        Application.EnableEvents = True
    End If
End Sub


Private Sub SortData(aRange As Range)
    Dim cel As Range
    Application.ScreenUpdating = False
    aRange.Resize(, 31).Sort aRange(1, 1), xlAscending
    For Each cel In aRange
        cel = cel.Row - 12
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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