VBA Macro - Automatic Sorting

Toncar58

New Member
Joined
Feb 24, 2021
Messages
30
Office Version
  1. 365
Platform
  1. MacOS
Hi

Can any help me please. I'm trying to automatically sort a column in alphabetical order using the following code

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("U:U")) Is Nothing Then
Range("IU1").Sort Key1:=Range("U2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

My data is in column "U" ... I used a similar code to automatically sot another worksheet in date order an it works perfectly.

When I first used the "Alphabetical" coding (which is the essence of my plea for help) it worked, but then I had to delete it (stupidly) and when I tried it again, it doesn't work.

Any help will be appreciated.

Toncar
 

Attachments

  • Screenshot 2021-07-30 at 14.50.31.png
    Screenshot 2021-07-30 at 14.50.31.png
    35.3 KB · Views: 7

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("U:U")) Is Nothing Then
Range("U2", Range("U" & Rows.Count).End(xlUp)).Sort Key1:=Range("U2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
 
Upvote 0
This is the code I used for sorting dates
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("I:I")) Is Nothing Then
Range("I1").Sort Key1:=Range("I2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

This works perfectly

I must be doing something wrong!
 
Upvote 0
I'm not sure ... if Im saving the macro correctly, and also deleting the macro's I previously used!

I say this because at first attempt it did work
 
Upvote 0
Is the macro in the sheet module that you want to sort?
 
Upvote 0
Yes, definitely
 

Attachments

  • Screenshot 2021-07-30 at 15.40.45.png
    Screenshot 2021-07-30 at 15.40.45.png
    109.4 KB · Views: 10
Upvote 0
What happens if you remove this line
VBA Code:
On Error Resume Next
 
Upvote 0
This is what I get
 

Attachments

  • Screenshot 2021-07-30 at 15.56.02.png
    Screenshot 2021-07-30 at 15.56.02.png
    53 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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