View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Michael[_3_] Michael[_3_] is offline
external usenet poster
 
Posts: 5
Default Selection.Sort problem with formulas

Well that is better but still no joy... for some reason, I now get a bunch
of blank rows at the top of the cell range after applying this sort. But
even more importantly, the BubbleSort wreaks havoc with the Range Names I
have defined (resulting in the formulas in the summary section all being
wrong afterwards).

I'm guessing I'm going to have to use a technique of some kind to cut and
paste the entire colored section instead of row-by-row... .of course, since
sections aren't the same size (number of rows) necessarily that's going to
make things even more complicated. (sigh)

The worksheet(s) I'm doing resembles this:

Row Color Data Sort
1 Blue 2 100
2 Blue 1 101
3 Blue 0 102
4 Green 1 200
5 Green 5 201

Where Blue and Green aren't columns but are the section colors (background
colors of the rows). Blue and Green are also the defined Range Names for the
appropriate rows (Blue = $1$Sort:$3$Sort, Green = $4$Sort:$5$Sort)! Sort is
a hidden column specifically for sorting the sections. The user can sort
ascending or descending by a button.

Now if you have a summary at the bottom of the sheet such as:

Blue = Sum(Blue)
Green = Sum(Green)

Then using the Sort method of Excel to sort the colored sections in
ascending order by the hidden Sort column, the summary formulas (and defined
Range Name cells) don't change to match the new sort order of the data.
Suddenly the Sum(Blue) is actually summing the green cells (now at rows 1
and 2) and 1 blue cell (now at row 3), and Sum(Green) is actually summing
the 2 remaining Blue cells at the bottom (rows 4 and 5). 8-(

Appling this Bubble Sort VBA code instead of the built in Sort method
however, all the data are sorted but I get a bunch of blank rows added in at
the top of the range AND my defined Range Names are all over the place (as
the rows were cut and pasted Excel tried to keep the range name definitions
in sync but failed miserably evidentally).

Since every section's Sort begins in increments of 100 (there are never more
than 100 rows in a section), can I use the 100's to decide sort order and
cut and paste the entire section instead of row-by-row? If so though, I have
another problem in that the sections aren't the same size so I can't simple
replace 1 section with another section as they may not fit (either too few
rows or too many rows!).

Ok I'm gonna take a break and go pull my hair out now. Ughhh! This is a
difficult task to get working I swear.

- Michael

"Joel" wrote in message
...
It was just menat as an example. This change will work.

from
Rows(RowCount2).Cut Destination:=Rows(RowCount1)
to
Rows(RowCount2).Cut
Rows(RowCount1).Insert (xlShiftDown)