Reply
 
Thread Tools Display Modes
  #1   Report Post  
Dan Corban
 
Posts: n/a
Default Sorting a table while keeping some related rows together

I have a table with two columns. The first column has names of items, each
item in its own row. I indent the items (using styles) if they are related to
the item directly above it. I want to sort this table, keeping the indented
item below the related item. I can't figure out how to do this. The indented
items just get sorted normally and end up separated from the related name.

An example of my table before sorting:
Lamp
Book
Moby Dick
Desk

When I sort, it looks like this:
Book
Desk
Lamp
Moby Dick

I want it to end up looking like this:
Book
Moby Dick
Desk
Lamp

Any idea how I can accomplish this (other than manually sorting)?

  #2   Report Post  
Jay Freedman
 
Posts: n/a
Default

Hi Dan,

To keep the related items together, they must be in the *same* row. In
your example, Book and Moby Dick have to be in the same cell.

You can prepare this by selecting a group of related items in the
first column and clicking Table Merge Cells. Then select the
matching group of cells in the second column and merge them (you can
just press F4 to repeat the action). Do this for each group of related
items.

After you sort, if you feel you need to return to separate rows, you
can do it with the Table Split Cell command. Unfortunately it won't
be as easy as the merge, since Word's default is to split the entry
into more columns in the same row, rather than more rows in the same
column. You'll have to change the settings in the Split dialog each
time.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Sat, 1 Jan 2005 00:39:03 -0800, "Dan Corban"
wrote:

I have a table with two columns. The first column has names of items, each
item in its own row. I indent the items (using styles) if they are related to
the item directly above it. I want to sort this table, keeping the indented
item below the related item. I can't figure out how to do this. The indented
items just get sorted normally and end up separated from the related name.

An example of my table before sorting:
Lamp
Book
Moby Dick
Desk

When I sort, it looks like this:
Book
Desk
Lamp
Moby Dick

I want it to end up looking like this:
Book
Moby Dick
Desk
Lamp

Any idea how I can accomplish this (other than manually sorting)?


  #3   Report Post  
Dan Corban
 
Posts: n/a
Default

Thanks for the reply. Your method seems close to what I want, but there are a
few problems.

When I only select the first column, I do not have the option to merge
cells. That command is greyed out. I must select the entire rows before merge
is an option.

Also, I sometimes have several levels of indentation. For example:

Book
1976
War and Peace
Moby Dick
1977
Star Wars

Ideally, the sublevels would be sorted as well. I am beginning to think that
I am asking too much for the built-in Word functions. Maybe there is a way to
do this programmatically?

"Jay Freedman" wrote:

To keep the related items together, they must be in the *same* row. In
your example, Book and Moby Dick have to be in the same cell.

You can prepare this by selecting a group of related items in the
first column and clicking Table Merge Cells. Then select the
matching group of cells in the second column and merge them (you can
just press F4 to repeat the action). Do this for each group of related
items.

After you sort, if you feel you need to return to separate rows, you
can do it with the Table Split Cell command. Unfortunately it won't
be as easy as the merge, since Word's default is to split the entry
into more columns in the same row, rather than more rows in the same
column. You'll have to change the settings in the Split dialog each
time.


  #4   Report Post  
Jay Freedman
 
Posts: n/a
Default

Hi Dan,

I don't understand the problem with selecting only cells in the first
column (specific cells, not the entire column) -- it works when I do
it.

That isn't going to matter to you anyway, because the second part of
your proposed method can't be done in Word except manually, line by
line, unless you care to do a lot of preparation.

I've done something of this sort to prepare a multilevel index for a
book, but I planned ahead and made the entries with everything in
place from the beginning. Because the finished index was some 8000
entries -- and because I was being paid to do the job -- it was worth
the effort.

First, each row must contain all the information from the preceding
levels. For your new example, I would have created these rows, perhaps
in this order:

Book, 1977, Star Wars
Book, 1977
Book, 1976, War and Peace
Book, 1976
Book, 1976, Moby Dick
Book

After all entries are created this way, I run a single sort over the
entire list. That puts the sample in this order:

Book
Book, 1976
Book, 1976, Moby Dick
Book, 1976, War and Peace
Book, 1977
Book, 1977, Star Wars

As part of the template for this index, I created several styles: a
base paragraph style, a one-indent paragraph style, a two-indent
paragraph style, and a hidden-font character style.

I also wrote a macro that examines each pair of rows, working from the
end of the document toward the beginning. If the two rows have the
same first two parts (using the comma as the separator between parts),
the two-indent style is applied to the second row, and the shared
parts are marked with the hidden style. If they have only the first
part in common, the one-indent style and the hidden style are applied.
If they have nothing in common, only the base style is used.

The macro is considerably more complex than the explanation implies
because it handles a number of special cases, so it wouldn't be
suitable for your problem as is. The general idea still applies,
though.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Sat, 1 Jan 2005 08:55:02 -0800, "Dan Corban"
wrote:

Thanks for the reply. Your method seems close to what I want, but there are a
few problems.

When I only select the first column, I do not have the option to merge
cells. That command is greyed out. I must select the entire rows before merge
is an option.

Also, I sometimes have several levels of indentation. For example:

Book
1976
War and Peace
Moby Dick
1977
Star Wars

Ideally, the sublevels would be sorted as well. I am beginning to think that
I am asking too much for the built-in Word functions. Maybe there is a way to
do this programmatically?

"Jay Freedman" wrote:

To keep the related items together, they must be in the *same* row. In
your example, Book and Moby Dick have to be in the same cell.

You can prepare this by selecting a group of related items in the
first column and clicking Table Merge Cells. Then select the
matching group of cells in the second column and merge them (you can
just press F4 to repeat the action). Do this for each group of related
items.

After you sort, if you feel you need to return to separate rows, you
can do it with the Table Split Cell command. Unfortunately it won't
be as easy as the merge, since Word's default is to split the entry
into more columns in the same row, rather than more rows in the same
column. You'll have to change the settings in the Split dialog each
time.


  #5   Report Post  
Klaus Linke
 
Posts: n/a
Default

Another approach I've used that worked pretty well:

Format the rows that should sort along with the previous row as "hidden", and set the view to not show hidden text.
If you sort now, the hidden rows will just move along with the preceeding visible rows.

In your case, it shouldn't be too hard to write a macro that loops all rows, checks for indents, and hides the row if it finds an
indent.

Greetings,
Klaus





"Jay Freedman" wrote:
Hi Dan,

I don't understand the problem with selecting only cells in the first
column (specific cells, not the entire column) -- it works when I do
it.

That isn't going to matter to you anyway, because the second part of
your proposed method can't be done in Word except manually, line by
line, unless you care to do a lot of preparation.

I've done something of this sort to prepare a multilevel index for a
book, but I planned ahead and made the entries with everything in
place from the beginning. Because the finished index was some 8000
entries -- and because I was being paid to do the job -- it was worth
the effort.

First, each row must contain all the information from the preceding
levels. For your new example, I would have created these rows, perhaps
in this order:

Book, 1977, Star Wars
Book, 1977
Book, 1976, War and Peace
Book, 1976
Book, 1976, Moby Dick
Book

After all entries are created this way, I run a single sort over the
entire list. That puts the sample in this order:

Book
Book, 1976
Book, 1976, Moby Dick
Book, 1976, War and Peace
Book, 1977
Book, 1977, Star Wars

As part of the template for this index, I created several styles: a
base paragraph style, a one-indent paragraph style, a two-indent
paragraph style, and a hidden-font character style.

I also wrote a macro that examines each pair of rows, working from the
end of the document toward the beginning. If the two rows have the
same first two parts (using the comma as the separator between parts),
the two-indent style is applied to the second row, and the shared
parts are marked with the hidden style. If they have only the first
part in common, the one-indent style and the hidden style are applied.
If they have nothing in common, only the base style is used.

The macro is considerably more complex than the explanation implies
because it handles a number of special cases, so it wouldn't be
suitable for your problem as is. The general idea still applies,
though.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Sat, 1 Jan 2005 08:55:02 -0800, "Dan Corban"
wrote:

Thanks for the reply. Your method seems close to what I want, but there are a
few problems.

When I only select the first column, I do not have the option to merge
cells. That command is greyed out. I must select the entire rows before merge
is an option.

Also, I sometimes have several levels of indentation. For example:

Book
1976
War and Peace
Moby Dick
1977
Star Wars

Ideally, the sublevels would be sorted as well. I am beginning to think that
I am asking too much for the built-in Word functions. Maybe there is a way to
do this programmatically?

"Jay Freedman" wrote:

To keep the related items together, they must be in the *same* row. In
your example, Book and Moby Dick have to be in the same cell.

You can prepare this by selecting a group of related items in the
first column and clicking Table Merge Cells. Then select the
matching group of cells in the second column and merge them (you can
just press F4 to repeat the action). Do this for each group of related
items.

After you sort, if you feel you need to return to separate rows, you
can do it with the Table Split Cell command. Unfortunately it won't
be as easy as the merge, since Word's default is to split the entry
into more columns in the same row, rather than more rows in the same
column. You'll have to change the settings in the Split dialog each
time.






  #6   Report Post  
Dan Corban
 
Posts: n/a
Default

I tried your method and it does not work for me. All hidden rows simply get
sorted to the top of the table.

Maybe I am missing a key step to your method, but this is what I did:
Show/Hide is off.
Select the entire row.
Go to Font properties and select "hidden".
Select Table - Sort and sort as I normally do.
Turn Show/Hide on.

I tried several variations of your method and the end result is always the
hidden rows sort to the top. I am using Word 2002. Maybe the hidden sort
behavior is different in another version?


"Klaus Linke" wrote:


Format the rows that should sort along with the previous row as "hidden", and set the view to not show hidden text.
If you sort now, the hidden rows will just move along with the preceeding visible rows.

In your case, it shouldn't be too hard to write a macro that loops all rows, checks for indents, and hides the row if it finds an
indent.


  #7   Report Post  
Klaus Linke
 
Posts: n/a
Default

Hi Dan,

You're right, it doesn't work for me in Word2003 either.
Not sure whether I was hallicunating, or whether I did somehow get it to work in the past.

Sorry!!

Plan C (counting Jay's method) might be:
Convert the table to tab-delimited text (...hope that's possible...), and then replace the paragraph marks in front of the paras
that should sort with the preceeding paragraphs with line breaks (Shift-Return).

If you can locate the rows that should sort with the preceeding "rows" with "Find/Replace", you can achieve that with two
"Find/Replaces":

-- Say they are in style "DontSort", then search for that style, check "Match wildcards", and put [!^13]@^13 in "Find what".
Replace with $$$^&.

-- Then replace ^p$$$ with ^l$$$.

If you finally have
E
D

B

then you get after sorting
B

E
D

and can replace the *l*ine breaks (^l) with *p*aragraph marks (^p) again.

Klaus


Dan Corban wrote:
I tried your method and it does not work for me. All hidden rows
simply get sorted to the top of the table.

Maybe I am missing a key step to your method, but this is what I did:
Show/Hide is off.
Select the entire row.
Go to Font properties and select "hidden".
Select Table - Sort and sort as I normally do.
Turn Show/Hide on.

I tried several variations of your method and the end result is always the
hidden rows sort to the top. I am using Word 2002. Maybe the hidden sort
behavior is different in another version?


"Klaus Linke" wrote:


Format the rows that should sort along with the previous row as "hidden", and set the view to not show hidden text.
If you sort now, the hidden rows will just move along with the preceeding visible rows.

In your case, it shouldn't be too hard to write a macro that loops all rows, checks for indents, and hides the row if it finds

an
indent.




  #8   Report Post  
Bob S
 
Posts: n/a
Default

On Sat, 1 Jan 2005 00:39:03 -0800, "Dan Corban"
wrote:

I have a table with two columns. The first column has names of items, each
item in its own row. I indent the items (using styles) if they are related to
the item directly above it. I want to sort this table, keeping the indented
item below the related item. I can't figure out how to do this. The indented
items just get sorted normally and end up separated from the related name.

An example of my table before sorting:
Lamp
Book
Moby Dick
Desk

When I sort, it looks like this:
Book
Desk
Lamp
Moby Dick

I want it to end up looking like this:
Book
Moby Dick
Desk
Lamp

Any idea how I can accomplish this (other than manually sorting)?



You might try the following (untested) process.

Copy the table to an empty document; this is for safety and for
non-interference.

Convert the table to text.

Assign the non-indented style an outline level of 1.

Assign the indented style an outline level of 2.

Go into Outline View. Set it to show level 1. Sort.

Go back to Normal View. Everything should look right.

Copy the results back to the real document. The styles ought to pick
up the definition from the original document, and lose the outline
levels. For paranoia, consider removing the levels from the style
definitions before copying the data back.

Convert back to table.


Bob S
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ALT+SHIFT+Arrow shortcut to move table rows Nicola Tables 3 December 16th 04 10:58 PM
using vba to hide rows in a table Jan Tables 2 December 6th 04 01:40 PM
Row height problem with table in table Stephanie T. Tables 0 November 23rd 04 04:35 PM
include all cells in sorting table Howie J. Tables 1 November 14th 04 03:27 PM
Can Table AutoFormat apply formatting to multiple heading rows? aubreyco Tables 1 November 10th 04 06:02 PM


All times are GMT +1. The time now is 10:47 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"