automatic sorting in excel using macros

CUP

New Member
Joined
Mar 24, 2003
Messages
3
Hi there..

I need some help in writing macros to sort the data (per one column) automatically whenever I change data in that column.

Thanks,
CUP
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Range("A1:D4").Select 'this is the sort range
    Selection.sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        
    Target.Select
End Sub
in the worksheet pane should get you started. Tweak as necessary.
 
Upvote 0
Hey.....

It is working but the problem is if I want to add one complete row then it should sort after I am done with adding new row. But here the code is activating in the middle of adding a new row..do you have any idea how to overcome this.........

CUP.
 
Upvote 0
Oops! Not sure how to handle that. Maybe some of the REAL gurus out there can help. I'm just a pseudo-guru.

Now my secret's out!
 
Upvote 0
Assumes column A with header in A1. Right click on sheet tab, left click on view code, paste in below code; click Alt+Q to return to worksheet.

Tested on XL2K2.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 1 Then Exit Sub
Range(("A1"), Cells(Rows.Count, 1).End(xlUp)).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=True, Orientation:=xlTopToBottom
End Sub
 
Upvote 0
i tried your macro and it worked fine when i changed them in the range but could u make one that would sort too when using the = formula

for example the range is A2:A10 and there is a cell outside that range that i connected to a cell in the range and when i change the cell outside the range the same number will be in the range but it doesn't sort automatically until i change another cell in the range
 
Upvote 0

Forum statistics

Threads
1,215,183
Messages
6,123,529
Members
449,105
Latest member
syed902

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