We help IT Professionals succeed at work.

Excel VBA for Groupings

1,068 Views
Last Modified: 2008-04-24
I have an excel file with groupings (the tree-view type of expanding nodes on the left side that let you show and hide things selectively).

I want to create an excel macro that copies the inside of each level 1 grouping to a new worksheet.  Anybody know where to start with something like this?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
As far as I know, you just have to set up a macro that copies the rows / columns into another worksheet, as if all of the outlines / groupings are expanded.  When everything is expanded, you see all of the rows / columns.  When they are collapsed, the row number jump from say 5 to 10, because that outline is hidden.  In the case that they are hidden though, a macro is still able to reference that cell.

For example, if row 5 was collapsed (hidden) you can still reference that row in VBA by using Rows(5:5).Copy

So, with everything expanded, you need to identify some pattern that distinguishes these levels, because I don't beleive there is any way to control the expanding or collapsing of a grouping in VBA.

Regards,

Rob.

Author

Commented:
I worked it out a bit and this is what I came up with.  I hope it helps someone in the future:

Sub CopyBlockLevel()
    'Activate the main worksheet
    Worksheets("Sheet1").Activate
   
    'Get the number of rows in the main worksheet
    Dim numRows As Integer
    numRows = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
   
    'Variables that mark the beginning and ending of a selection
    Dim startSelectRow As Integer
    startSelectRow = 1
    Dim endSelectRow As Integer
    endSelectRow = 1
   
    'Other variables
    Dim i As Integer
    Dim marked As Boolean
    marked = False
   
    Dim WS As Worksheet
   
    'Go through each of the rows
    For i = 1 To numRows
       
        'Looks for outline Level 2
        Dim t As Variant
        t = Worksheets("Sheet1").Rows(i).OutlineLevel
       
        If Worksheets("Sheet1").Rows(i).OutlineLevel >= 2 And marked = False Then
            startSelectRow = i 'When it finds outline level 2, it marks it
            marked = True
        ElseIf Worksheets("Sheet1").Rows(i).OutlineLevel < 2 And marked = True Then
            endSelectRow = i - 1 'Once the row is no longer outlinelevel 2, we can get the ending row of the block of the outline
            marked = False
           
            'Select and copy the block of level-two outline
            Worksheets("Sheet1").Range("A" & startSelectRow & ":Z" & endSelectRow).Select
            Selection.Copy
           
            'Create a new worksheet, activate it and paste
            Set WS = Sheets.Add
           
            WS.Activate
            WS.Cells(1, 1).Select
            Selection.PasteSpecial xlPasteValues
           
            'Go back to Sheet1 for the rest of the level-two elements
            Worksheets("Sheet1").Activate
                   
        End If
       
    Next i

End Sub
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Great.  Well done.

Rob.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.