473,378 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Collapse and Expand Rows in Excel

124 100+
I know this is the wrong forum for this post, but I posted the same question to the Bytes Excel forum and received no response. Also, I know there are a number of developers on this board who are experts in Excel and this is a continuation of my last "Sort Angst" post on this forum.

I have a spreadsheet of 75,000 rows that is sorted and grouped by AcctNum. There are several records for each AcctNum. The user wants to be able to collapse records so that only the first record for that AcctNum displays and the other records for that AcctNum are hidden. Then when the user clicks anywhere in that first AcctNum record, all of the other records for that AcctNum are displayed. Is it possible to do this? I have experience with Access and VBA, but I'm not sure how to get this done in Excel. Any advice is welcome.
Apr 7 '17 #1

✓ answered by NeoPa

I just had a view but I still can't see any question there. It's the same for all in that forum. I see all answers but never the first post.

NB. Expanding and collapsing by group is a single-step process. There's no need to handle each group individually.

OTOH what you're asking for is relatively straightforward in VBA anyway so let's see.

Assuming you have your AcctNum in column A and each Row has a vale set for AcctNum then the following code should work for you :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Sub GroupCollapse()
  4.     Dim lngRow As Long, lngTop As Long
  5.  
  6.     lngRow = 1
  7.     Do Until Range("A" & lngRow) = ""
  8.         lngTop = lngRow
  9.         Do
  10.             lngRow = lngRow + 1
  11.         Loop While Range("A" & lngRow) = Range("A" & lngTop)
  12.         If lngRow > lngTop + 1 Then _
  13.             Rows(lngTop + 1 & ":" & lngRow - 1).Hidden = True
  14.     Loop
  15. End Sub
  16.  
  17. Public Sub GroupExpand()
  18.     Rows.Hidden = False
  19. End Sub
I assume you can handle triggering these routines as and when.

5 1826
NeoPa
32,556 Expert Mod 16PB
Excel has an inbuilt feature that allows you to collapse and expand groups. However! This hides all the sub records and doesn't leave one showing. It allows you to show aggregated data for the group but no individual details.

If you feel this is acceptable then fine. It should be easy enough simply to tell the users how to do that using the standard interface.

Otherwise code will be required. Rows can be hidden and made visible either on a row-by-row basis or by a range of rows.

Let us know what you need to continue.

PS. I can't even see the questions in the Excel forum otherwise I'd look there myself.
Apr 7 '17 #2
BikeToWork
124 100+
Thanks for the response, NeoPa. The user interface will not work because there are thousands of distinct AcctNum's and it would take forever to group them manually. How would I go about doing this with Excel VBA? I want the first row for an AcctNum to remain visible when the other rows with the same AcctNum are collapsed. Upon clicking the plus sign, all rows for the group are displayed. So basically, I want to group the second row for an AcctNum up to the last row for that AcctNum together so that the first row is still displayed when the group is collapsed. Any advice is welcome. The post on the Excel forum had over 100 views, but no responses.

https://bytes.com/topic/excel/answer...-collapse-rows
Apr 7 '17 #3
NeoPa
32,556 Expert Mod 16PB
I just had a view but I still can't see any question there. It's the same for all in that forum. I see all answers but never the first post.

NB. Expanding and collapsing by group is a single-step process. There's no need to handle each group individually.

OTOH what you're asking for is relatively straightforward in VBA anyway so let's see.

Assuming you have your AcctNum in column A and each Row has a vale set for AcctNum then the following code should work for you :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Sub GroupCollapse()
  4.     Dim lngRow As Long, lngTop As Long
  5.  
  6.     lngRow = 1
  7.     Do Until Range("A" & lngRow) = ""
  8.         lngTop = lngRow
  9.         Do
  10.             lngRow = lngRow + 1
  11.         Loop While Range("A" & lngRow) = Range("A" & lngTop)
  12.         If lngRow > lngTop + 1 Then _
  13.             Rows(lngTop + 1 & ":" & lngRow - 1).Hidden = True
  14.     Loop
  15. End Sub
  16.  
  17. Public Sub GroupExpand()
  18.     Rows.Hidden = False
  19. End Sub
I assume you can handle triggering these routines as and when.
Apr 7 '17 #4
BikeToWork
124 100+
Thanks, NeoPa. That worked a treat. I didn't realize this before but Excel puts a 1 and 2 at the top left of the spreadsheet where you can expand and collapse all rows by selecting the 1 or the 2. I only had to change a few lines in your code. The line:

Rows(lngTop + 1 & ":" & lngRow - 1).Hidden = True

I changed to Rows(lngTop + 1 & ":" & lngRow - 1).Group

Thanks again for the advice.
Apr 7 '17 #5
NeoPa
32,556 Expert Mod 16PB
That sounds like what I was referring to in the first place. Did you try selecting the data then clicking on the Data ribbon and selecting Subtotal? That will handle much of the work for you without code. Unfortunately, it doesn't actually give you the first line but does allow you to select the 1, 2 or 3 to show the level of data required.

Personally, I believe my posted code most closely matches your original request but there are options there so choose the one you like the most.
Apr 8 '17 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: jaykchan | last post by:
I am looking for a control that allows our users to expand and collapse rows in a report. Currently, the report is being shown in a web page generated from a CGI script. Unfortunately, the...
2
by: alexyz | last post by:
Hi everyone, I write a simple javascript to expand / collapse some rows of a table. The problem is that when I click more than one time the link to expand /collapse the rows I get an unwanted...
1
by: pej.odonnell | last post by:
Hello everyone, 2 Issues: 1. When treeview renders all parent nodes are open? I want it to render closed? 2. When I try to collapse programmatically, it does not do anything? I'm using a...
1
by: Tamer EL MORSI | last post by:
hello there is there a way to collapse-expand part of my text the problem is that i am retriving text data from my data base from a field named "Description" and i want to display the first ...
0
by: Newfie000 | last post by:
Hello All, I'm new to reporting services. I need a table of results defaulted to show only a number of rows (say 5). But then an option to expand the full results. Background: I have five...
6
by: philmetz | last post by:
Hi Does anyone have a simple way to expand and collapse table rows. For example i will have 26 rows (one for each letter of alphabet) then i can say expand A and i will get a list of things there?...
4
by: koyanpaing | last post by:
hello everyone, I have a problem with collapse table multiple rows. I don't want to expend at first. When i click the link and at that time, rows must be expend. But in the attached file the...
0
by: cottonbody | last post by:
Hi all, I am new to this. How do make the list in one div collapse when the other one expand? Here is what i have so far. <body> <div id="Section1"><ul onclick = "this.className =...
2
by: scrapcode | last post by:
Hi guys, this should be an easy one. I have an Excel workbook that I need to run a filter with lots of criteria on a number of different sheets and count the results every day. I'm looking to...
1
by: oconrl4 | last post by:
I need to know how to expand the number of rows to import into an access database from a report. The limit is currently set to 100 rows. The access database is 2003. I am new to access. The users...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.