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: 6

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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

Forum statistics

Threads
1,223,579
Messages
6,173,170
Members
452,504
Latest member
frankkeith2233

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