Automate Sorting Row Vertically

KatieB24

New Member
Joined
Nov 6, 2024
Messages
2
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
Platform
  1. Windows
Is there a way to automate the sorting of a row a-z ascending but across not down. I add a new column in once a week and use a script to make it quicker however i was hoping i could always ensure the row is organised ascendingly without having to do anything?

For example

Row 1 Week1 Week2 Week3 Week4
I would like these columns to remain in order so when i add a new column for example week5 no matter where i insert the column it can update to ensure week 5 goes to the end?
 

Attachments

  • Capture.PNG
    Capture.PNG
    6.3 KB · Views: 10

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Thankyou for your response. I know how to sort. I am asking is there a way to automate it so that it always occurs not when i manually have to do it
 
Upvote 0
Worksheet event VBA code enclosed.
Start Cell for week number entry is taken as B1. Change if required.
Code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("1:1")) Is Nothing Then
If Target.Cells.Count > 1 Then Exit Sub
On Error GoTo Line1
Application.EnableEvents = False
Dim Lc&, Lr&, clms&, S$, Stcel As Range, Cel As Range
Set Stcel = Range("B1")
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
clms = Lc - Stcel.Column + 1
Lr = Range("A" & Rows.Count).End(xlUp).Row
K = InStr(1, Stcel, " ") + 1
S = Left(Stcel, K - 1)
For Each Cel In Range("b1").Resize(1, clms)
Cel.Value = Val(Mid(Cel, K))
Next

ActiveSheet.Sort.SortFields.Clear
 ActiveSheet.Sort.SortFields.Add2 Key:=Stcel.EntireRow _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Stcel.Resize(Lr, clms)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
For Each Cel In Range("b1").Resize(1, clms)
Cel.Value = S & Cel
Next
Line1:
Application.EnableEvents = True
End If
End Sub



How to use worksheet event the code
Right click on Sheet tab --> view code
Visual Basic (VB) window opens.
Paste the code
Close the VB window.
Save the file as .xlsm
 
Upvote 0
To Sheet code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim s$, e$, x, i&, ii&, temp, r As Range
    If Target.Row > 1 Then Exit Sub
    s = [iferror(substitute(address(1,min(if(left(1:1,2)="WK",column(1:1))),4),1,""),"")]
    e = [iferror(substitute(address(1,max(if(left(1:1,2)="WK",column(1:1))),4),1,""),"")]
    If (s = "") + (e = "") + (s = e) Then Exit Sub
    Application.EnableEvents = False
    Set r = Intersect(UsedRange, Columns(s & ":" & e))
    x = r.Rows(1).Value
    For i = 1 To UBound(x, 2) - 1
        For ii = i + 1 To UBound(x, 2)
            If Val(Mid$(x(1, i), 3)) > Val(Mid$(x(1, ii), 3)) Then
                temp = x(1, i): x(1, i) = x(1, ii): x(1, ii) = temp
            End If
    Next ii, i
    With Me.Sort
        .SortFields.Clear
        .SortFields.Add2 Key:=r.Rows(1), Order:=1, _
        CustomOrder:=Join(Application.Transpose(Application.Transpose(x)), ",")
        .SetRange r
        .Orientation = 2
        .Apply
    End With
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,512
Members
452,650
Latest member
Tinfish

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