1. As mentioned, an Excel (structured) Table automatically keeps track of its own dimensions (unlike a basic contiguous range of cells as we had to deal with before XL2010, where we used Defined Names with formula to scope the boundaries of the target range), which then allows for easy referencing in VBA code.
2. I must also apologise
, for whilst I was writing this post I realised I was actually thinking about another great Excel feature that uses grouping - Subtotals!! You may wish to consider this because you can apply it to (or remove it from) the entire database in one go (the primary aspect I was thinking about) - though a separate pass is required for each additional level required. The functionality inserts a subtotal row (and formula using a variety of aggregating functions) on each change in the field being subtotalled, and simultaneously groups the subsets. You can also choose whether to have the subtotals/parent row above or below the children.
So rather than "adjust" subsets of your existing Grouping (and having to loop through individual rows, which is fiddly) whenever you add new rows, what I'm suggesting is that your macro:
- completely removes all existing grouping (Subtotals) from the entire database
- re-sorts the entire database as required to enable the grouping required (multiple levels of sorting if necessary to support multiple levels of grouping)
- re-apply grouping (Subtotals) at the levels required. If I recall correctly (it's a while since I've actually had to use this functionality) you need to apply the highest lowest level subtotals first, and then work down to the lowest.
Check out this video for a good demonstration:
https://www.youtube.com/watch?v=N8WPJ6fBJks
All of this would be just an automation of the steps you'd do manually - but would complete it in the blink of an eye. Remember that Grouping is just an "overlay" that sits on top of your original data - and even though Automatic subtotals does actually insert subtotal rows, it can also remove them to reinstate your data range to its original state.
Note also that whilst Subtotals adds formula to Sum, Count, etc. your subgroups, if you just want the grouping/outline you can easily remove these with a few extra steps:
- Collapse the outline so just the lowest level (data) rows are hidden, thus displaying ALL subtotal rows
- Select the entire range
- Press F5 (Goto) and click the Special button at the bottom left (Note that there is a button you can add to your Quick Access Toolbar which will do this with one click)
- Click the "Visible cells only" radio button to select it (deselects the 'hidden' cells)
- Press Delete to clear the formula whilst leaving the outline in place.
3. Re marking a thread closed/solved:
The only thing you can do is edit the Title, say by prefixing the original with "SOLVED:"