Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / April 2008

Tip: Looking for answers? Try searching our database.

Code to automatically sort a list, each time that a new entry is     added to the bottom of the list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike C - 13 Apr 2008 06:04 GMT
Hello - I am wondering if anyone could provide me with code that will
re-sort a list, each time a user places a new line at the bottom of
the list.

So, for example, in column A, rows 1-10, I have a list of names, which
are sorted descending.  When a new name is added to row 11, and the
user clicks out off of the cell or presses enter, I am hoping the
column can automatically resort.

Thanks for any suggestions.
Per Jessen - 13 Apr 2008 06:21 GMT
> Hello - I am wondering if anyone could provide me with code that will
> re-sort a list, each time a user places a new line at the bottom of
[quoted text clipped - 6 lines]
>
> Thanks for any suggestions.

Hi Mike

Place the code below in the code sheet of the sheet containing your
list.

Private Sub Worksheet_Change(ByVal Target As Range)
Set isect = Intersect(Target, Range("A:A"))
If Not isect Is Nothing Then
   Range("A:A").Sort Key1:=Range("A1"), Order1:=xlAscending,
Header:=xlGuess, _
       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End If
End Sub

Regards,
Per
Mike C - 13 Apr 2008 16:12 GMT
> > Hello - I am wondering if anyone could provide me with code that will
> > re-sort a list, each time a user places a new line at the bottom of
[quoted text clipped - 23 lines]
> Regards,
> Per

Thanks Jessen.  For some reason, the section that reads:

Range("A:A").Sort Key1:=Range("A1"), Order1:=xlAscending,
Header:=xlGuess, _
       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

is giving me an error (i.e., turning red, reading "expression error")
as soon as I paste it into the worksheet's module.

Is this possibly because i have excel 2003?
Per Jessen - 13 Apr 2008 16:34 GMT
Hi

The problem is wordwrap in your reader.

Remove the carriage return after xlAscending,

so the section is only two lines.

> Thanks Jessen.  For some reason, the section that reads:
>
[quoted text clipped - 8 lines]
>
> - Vis tekst i anførselstegn -

Regards,
Per
JLGWhiz - 13 Apr 2008 16:59 GMT
Mike, that should all be one line of code.  The automatic line wrap in the
news reader screwed it up.

> > > Hello - I am wondering if anyone could provide me with code that will
> > > re-sort a list, each time a user places a new line at the bottom of
[quoted text clipped - 34 lines]
>
> Is this possibly because i have excel 2003?
Peter T - 13 Apr 2008 16:40 GMT
Try this, as written:

- Sort should update after user leaves the cell (could update immediately on
change but this way allows quick re-edit/undo of the cell).
- If there is a gap or empty cell in the range and the changing cell is
below the gap nothing will happen (allows for a formula cell to be written 2
or more cells below the range)
- if user changes a cell that's already in the sorted range the range will
also re-sort (after leaving the cell)

following goes in the Worksheet module (rt-click sheet tab, view code)

Dim mLastLastRow As Long
Dim msLastActiveAddr As String
Dim mRng2Sort As Range
Const sTopCell As String = "A2" ' << CHANGE

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   UpdateSorter
End Sub

Public Function UpdateSorter()
Dim sLast As String
Dim nRow As Long
Dim cel As Range

   Set cel = ActiveCell
   sLast = cel.Address

   If sLast <> msLastActiveAddr Then
       msLastActiveAddr = sLast

       If Not mRng2Sort Is Nothing Then

           mRng2Sort.Sort Key1:=mRng2Sort(1, 1), _
                          Order1:=xlAscending, _
                          Header:=xlNo, _
                          OrderCustom:=1, _
                          MatchCase:=False, _
                          Orientation:=xlTopToBottom

       End If

   End If

   With Range(sTopCell)
       If cel.Column = .Column Then
           nRow = .End(xlDown).Row
           If nRow = Rows.Count Then nRow = .Row

           If cel.Row >= .Row And cel.Row <= nRow + 1 And nRow > .Row Then
               Set mRng2Sort = Range(Range(sTopCell), Cells(nRow, .Column))
           Else
               Set mRng2Sort = Nothing
           End If
       End If
   End With

End Function

Regards,
Peter T

> Hello - I am wondering if anyone could provide me with code that will
> re-sort a list, each time a user places a new line at the bottom of
[quoted text clipped - 6 lines]
>
> Thanks for any suggestions.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.