Auto Sorting Alphabetically VBA or other

Avalanchez

New Member
Joined
Jun 23, 2021
Messages
14
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Platform
  1. Windows
Hello I have a work sheet that has columns A-AZ. Each of these columns has a header with a list of names under it. I want to add a name and have it automatically alphabetized and sorted when im done typing the new name.

I have been tryin this code:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("B1", Range("B1").End(xlDown)).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlNo
End Sub
Which seems to do the job just fine but when I try and set it up for each column it lags super hard and errors out.
How can I use or re write this code or similar to sort all of these columns?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this code instead:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim cl As Long
    Dim rw As Long
    
'   Only run when a single cell is manually updated
    If Target.CountLarge > 1 Then Exit Sub
    
'   Find cl where cell just updated
    cl = Target.Column
    
'   Find last row in column with data
    rw = Cells(Rows.Count, cl).End(xlUp).Row
    
'   Sort column
    Range(Cells(1, cl), Cells(rw, cl)).Sort Key1:=Cells(1, cl), Order1:=xlAscending, Header:=xlYes
    
End Sub
 
Upvote 0
Solution
Try this code instead:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
    Dim cl As Long
    Dim rw As Long
   
'   Only run when a single cell is manually updated
    If Target.CountLarge > 1 Then Exit Sub
   
'   Find cl where cell just updated
    cl = Target.Column
   
'   Find last row in column with data
    rw = Cells(Rows.Count, cl).End(xlUp).Row
   
'   Sort column
    Range(Cells(1, cl), Cells(rw, cl)).Sort Key1:=Cells(1, cl), Order1:=xlAscending, Header:=xlYes
   
End Sub
That works great!

Thanks Boss!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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