PDA

View Full Version : [SOLVED:] Automatic re-sorting of data



K. Georgiadis
04-22-2005, 01:19 PM
I have a large data list in range A2:P76 of Sheet1 (with row 2 containing the column headings) and the list is sorted in descending order according to the values in column P.

There is the strong likelihood that the data will change several times, requiring resorting of the list. Is it possible to have VBA code in Sheet1 that automatically resorts each time the sheet is activated, or am I better off recording a macro and providing the user with a resorting button?

Jacob Hilderbrand
04-22-2005, 01:46 PM
Try this:



Option Explicit

Private Sub Worksheet_Activate()
Range("A2:P76").Sort Key1:=Range("P2"), Order1:=xlDescending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub

TheAntiGates
04-22-2005, 02:02 PM
That sort is nearly instantaneous, isn't it? So the worst cost is probably a "blinking" or perhaps the "Want to save?" prompt when nothing has changed. That would weigh against the cost of the user "not liking" using the button.

The same code you would record for a button would be what you put in the Activate event code, as DRJ shows above.

Whatever is coded, putting A2:P76 in the code might bite you later. Define a named range over that rectangle, and refer to that in the code, if there's a chance the dimensions will change.

K. Georgiadis
04-22-2005, 03:25 PM
Thanks DRJ, I'll give it a try. Also, thanks "The AntiGates;" excvellent point about using a named range

Jacob Hilderbrand
04-22-2005, 05:31 PM
You're Welcome :beerchug:

Take Care

K. Georgiadis
04-23-2005, 08:09 PM
I thought that using a dynamic named range was a good idea, but then I discovered I didn't really know how to use it in the code provided by DRJ :(

My sheet containing the code for an automatic re-sort is now called "Matrix" and the data in A2:P76 is named "Dynarange," defined by the following formula:

=OFFSET(Matrix!$A$2,0,0,COUNTA(Matrix!$A:$A,16))

16 being the number of columns in the data list. I often use this type of formula for named ranges that govern Pivot Tables, so that the Pivot Tables automatically adjust for data additions to a list.

I quickly found out that I cannot simply replace "Dynarange" for "A2:P76" on line 1 of DRJ's code and leave everything else the same. Is different syntax needed for a dynamic range?
Does Dyanarange need to be declared as variant?

Jacob Hilderbrand
04-23-2005, 09:09 PM
It should work fine, but your Dynamic Range is incorrect. The 16 should not be inside the CountA formula.


Option Explicit

Private Sub Worksheet_Activate()

Range("Dynarange").Sort Key1:=Range("P2"), Order1:=xlDescending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub


=OFFSET(Matrix!$A$2,0,0,COUNTA(Matrix!$A:$A),16)

K. Georgiadis
04-24-2005, 05:50 AM
Duh!!!! You're absolutely correct (as usual). I typed the formula incorrectly -- haste makes waste :banghead: Thanks!!!