View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Gord Dibben Gord Dibben is offline
external usenet poster
 
Posts: 22,906
Default Using Macro to sort without clicking on macro button

You could use change event code in the worksheet but this negates the "undo"
feature so if you have made a mistake, your sort has taken place as soon as you
hit the ENTER button so makes it hard to troublesheet for errors in data entry.

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ws_exit:
If Intersect(Range(Target(1).Address), _
Range("A:A, C:C")) Is Nothing Then Exit Sub
Set OldCell = Target
Application.EnableEvents = False
Columns("C:C").Select
Selection.Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Columns("A:A").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
OldCell.Select
ws_exit:
Application.EnableEvents = True
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code".

Copy/paste into that sheet module.


Gord Dibben MS Excel MVP

On 2 May 2007 10:32:21 -0700, dd wrote:

Hi,
I have four columns of data. I want to sort Col C in ascending order,
then Col A in ascending order. When someone enters a name in Col C,
they would have to click on the macro button to resort the data. So
instead of clicking on the macro button, is there a way to
automatically resort the data after it is entered in Col C or Col A.
Thanks for your help.