VBA to Collapse Rows (3 part question)

Kellie220

New Member
Joined
Jan 23, 2024
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi! I am looking for a VBA code that will hide/unhide (or) Collapse/Expand rows in a worksheet I am using. Column B is "ItemID". We have multiple ItemIDs with different Serial#'s. We would like to be able just to see ItemID once but have the ability to expand and see the multiple serial#'s attached. We do input the ItemId over and over to capture the different Serial#'s. The first entry we do is the main entry, that would be the one we would want to see. Everytime we enter an ItemID, that is the main entry, every other row is for more unique data in other columns that are associated with the "ItemID".
We would would prefer VBA over the filter collapse/expand). The view we typically use is the collapsed view, but we do have the need to see the expanded version. (This is the First question).

(Second question) Our workbook has 2 worksheets. We input info on worksheet one, then collapse the data (very old school method), then copy and paste it into worksheet 2 at the bottom. We would like to not have to always copy and paste the collapsed info. To have it just transfer over to Sheet 2 into the next available rows. We would like to have worksheet 1 drive worksheet 2. If changes are made on worksheet 1 then worksheet 2 changes. Also have whatever information transferred to worksheet 2 to not be editable. (You have to go to worksheet 1 to do edits on those columns). Our Sheet 1 just has our initial entry, only about 15 columns, where as sheet 2 is where we do the actual work and it has over 150 columns. We would like all of the information in the columns on Sheet 1 to be transfer into sheet 2 **If this could be based on column count just in case sometimes we add an additional column into sheet 1 we want it to then reflect into sheet 2.
(Last question) for both sheets to be able to automatically accept new entries without having to expand the table. Below is a very small example. Our current worksheet has over 30 thousand lines and 150 columns. I tried to condense since the ask is only formatting.

Any help would be appreciated. :)

Based on Column B "ItemID" - Below is what we would like (collapsed):
DEPTItemIDSERIALDUE DateCAP/OMPOPriorityTier LevelCreatedDate
84RO2112206SL-1691510E9/30/2023CAP2725938032NonThreat9/30/2022
84RO2112714SL-1690751E11/7/2023CAP2726865762NonThreat11/7/2022
84RO2112737SL-1688824E11/16/2023CAP2727065362NonThreat11/16/2022
84RO2112870SL-1693226E9/1/2023CAP2725370302NonThreat9/1/2022
70RO2115078SL-429189S3/31/2023CAP2725537112NonThreat9/11/2022
16RO2115618SL-1997834E9/14/2022CAP2703468572NonThreat9/14/2019
150RO2115634SL-45012S8/20/2022CAP3888185172NonThreat9/20/2017
87RO2117038UG-517463812/31/2023CAP2728727642NonThreat3/1/2023
87RO2117132SL-59367S12/30/2023CAP2729051152NonThreat3/20/2023

Based on Column B "ItemID" - This is Expanded
DEPTItemIDSERIALDUE DateCAP/OMPOPriorityTier LevelCreatedDate
84RO2112206SL-1691510E9/30/2023CAP2725938032NonThreat9/30/2022
84RO2112714SL-1690751E11/7/2023CAP2726865762NonThreat11/7/2022
84RO2112714SL-1688781E10/7/2023CAP2726164792NonThreat10/7/2022
84RO2112714SL-1695334E10/5/2023CAP2726130052NonThreat10/6/2022
84RO2112737SL-1688824E11/16/2023CAP2727065362NonThreat11/16/2022
84RO2112870SL-1693226E9/1/2023CAP2725370302NonThreat9/1/2022
84RO2112870SL-1693317E12/18/2023CAP2727613972NonThreat12/17/2022
70RO2115078SL-429189S3/31/2023CAP2725537112NonThreat9/11/2022
16RO2115618SL-1997834E9/14/2022CAP2703468572NonThreat9/14/2019
150RO2115634SL-45012S8/20/2022CAP3888185172NonThreat9/20/2017
87RO2117038UG-517463812/31/2023CAP2728727642NonThreat3/1/2023
70RO2117038SL-4373647E12/31/2023CAP2726204912NonThreat10/10/2022
150RO2117038SL-2292235E8/20/2022CAP3888185422NonThreat9/20/2017
87RO2117132SL-59367S12/30/2023CAP2729051152NonThreat3/20/2023
87RO2117132UG-527920812/31/2023CAP2728726622NonThreat3/1/2023
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Ad 1. Run the macro one time from the Macro window. Then click on the button.
VBA Code:
Sub Toggle()
    Dim v As Variant
    Dim oDic As Object
    Dim oDicRows As Object
    Dim i As Long
    Dim lCnt As Long
    Dim shpToggle As Shape


    On Error Resume Next
    Set shpToggle = ActiveSheet.Shapes("tgl_Button")
    On Error GoTo 0

    If shpToggle Is Nothing Then
        Set shpToggle = AddButton
    End If

    If shpToggle.TextFrame2.TextRange.Text = "Collapse" Then
        shpToggle.TextFrame2.TextRange.Text = "Expand"

        Set oDic = CreateObject("Scripting.Dictionary")
        Set oDicRows = CreateObject("Scripting.Dictionary")

        With Range("A1").CurrentRegion
            v = .Offset(1).Resize(.Rows.Count - 1).Columns(2).Value
        End With


        On Error Resume Next

        For i = 1 To UBound(v)
            Err.Clear
            oDic.Add v(i, 1), Empty
            If Err.Number <> 0 Then
                oDicRows.Add i + 1, Empty
            End If
        Next i

        If UBound(oDicRows.Keys()) > -1 Then
            v = oDicRows.Keys()
            For i = 0 To UBound(v)
                Rows(v(i)).Hidden = True
            Next i
        End If
    Else
        shpToggle.TextFrame2.TextRange.Text = "Collapse"
        Rows.Hidden = False
    End If
End Sub


Private Function AddButton() As Shape
    Dim Shp As Shape

    Set Shp = ActiveSheet.Shapes.AddShape(msoShapeBevel, 154, 5, 92, 25)
    
    With Shp
        .TextFrame2.TextRange.Text = "Collapse"
        .TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
        .TextFrame2.VerticalAnchor = msoAnchorMiddle
        .Name = "tgl_Button"
        .OnAction = "Toggle"
    End With

    Set AddButton = Shp
End Function
Artik
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top