PC Review


Reply
Thread Tools Rate Thread

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

 
 
Mike C
Guest
Posts: n/a
 
      13th Apr 2008
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.
 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      13th Apr 2008
On 13 Apr., 07:04, Mike C <js2k...@yahoo.com> wrote:
> 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.


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
 
Reply With Quote
 
Mike C
Guest
Posts: n/a
 
      13th Apr 2008
On Apr 13, 12:21*am, Per Jessen <perjesse...@hotmail.com> wrote:
> On 13 Apr., 07:04, Mike C <js2k...@yahoo.com> wrote:
>
> > 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.

>
> 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


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?

 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      13th Apr 2008
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:
>
> 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?- Skjul tekst i anførselstegn-
>
> - Vis tekst i anførselstegn -




Regards,
Per
 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      13th Apr 2008
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

"Mike C" <(E-Mail Removed)> wrote in message
news:269119e8-37ad-4694-81c6-(E-Mail Removed)...
> 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.



 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      13th Apr 2008
Mike, that should all be one line of code. The automatic line wrap in the
news reader screwed it up.

"Mike C" wrote:

> On Apr 13, 12:21 am, Per Jessen <perjesse...@hotmail.com> wrote:
> > On 13 Apr., 07:04, Mike C <js2k...@yahoo.com> wrote:
> >
> > > 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.

> >
> > 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

>
> 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?
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
alpha-sort list where each entry contains more than one line Jean Chicago Microsoft Word Document Management 1 18th Jan 2010 07:00 PM
list automatically scrolls to bottom Sacramento Microsoft Outlook Discussion 4 4th Nov 2008 09:10 AM
New Entry in list view goes to bottom =?Utf-8?B?Q2FuZGljZQ==?= Microsoft Outlook Calendar 0 19th Dec 2005 06:58 PM
Any entry in Global Address goes to bottom of list =?Utf-8?B?Qnlyb25fR3JpZmZpbmc=?= Microsoft Outlook Contacts 0 20th Apr 2005 10:17 PM
how do i automatically sort a list =?Utf-8?B?RGVmb2VzIFJpZ2h0IEJvb3Q=?= Microsoft Excel Worksheet Functions 0 3rd Feb 2005 02:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:13 PM.