Consulting

Results 1 to 2 of 2

Thread: Macro to Group columns on the basis of criteria

  1. #1

    Macro to Group columns on the basis of criteria

    Hi All,

    I am currently trying to write a macro which will group columns in a specified range on the basis of a criteria for e.g. I have a range from Columns A to D then my macro will be looking into the 4th row of each column and see if there is a zero value or blank in the same if for e.g. If A4 has zero value and B4 also has zero value then it will group A4 and B4 columns together or if only A4 has value zero then it will hide A column and this way it will loop through the entire column range specified and do the grouping. Following is the code which I have currently with me.

    [VBA]Sub AutoGroup()

    Application. ScreenUpdating = False

    Dim intFirstColumn As Integer
    Dim intLastColumn As Integer


    intFirstColumn = 0

    'Format rows and columns
    Columns("B:Z").Select


    'Set AutoFilter


    Range("B4").Select

    Do Until ActiveCell.Value <> 0

    If ActiveCell.Value = ActiveCell.Offset(0, 1).Value Then
    'First Match
    If intFirstColumn = 0 Then intFirstColumn = ActiveCell.Row + 1
    'If not the first match, do nothing. Continue on.
    Else
    'If intFirstColumn is not 0, and the next cell does not match,
    'we've reached the end of the group. Create the group.
    If intFirstColumn <> 0 Then
    intLastColumn = ActiveCell.Row
    Columns(intFirstColumn & ":" & intLastColumn).Select
    Selection.Columns.Group
    intFirstColumn = 0
    Range("A" & intLastColumn).Select
    End If
    'If intFirstColumn = 0, and the next cell does not match, this
    'cell is unmatched, and there is no group. Continue on.

    End If

    ActiveCell.Offset(0, 1).Select

    Loop



    Application.ScreenUpdating = True
    End Sub[/VBA]

    Thanks a lot for your help in advance.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    please mention when you cross-post where you have done so..
    so far here:
    http://www.mrexcel.com/forum/showthread.php?t=477306
    anywhere else?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •