Group Data Based on Blank Cell value in column

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
I would like to group data based on blank cell values in column B. I want to group cells by counting up from the last active value in column B and then grouping the data below this blank cell. I would like for the groups to end up collapsed at the end of the macro. The data varies in length from day to day. Around 5000 rows. The groups would be based on customers. So a collapsed data set would be ideal. YOu could uncollapse/open them to view the detail. My data looks like this:


Excel 2010
ABCDEFG
1CodeCompany NameWO NumberWO TypeRef NumberTail NumberStatus
2AAR ACS-NY C/O AAR CORP
3AARALAAR ACS-NY C/O AAR CORPU065907Internal311017816/9112547CLOSED
4AARALAAR ACS-NY C/O AAR CORPU067452-28Internal312001928/9115687Closed
5AARALAAR ACS-NY C/O AAR CORPU067452-2Internal312001928/9115687Closed
6AARALAAR ACS-NY C/O AAR CORPU065908Internal311017816/9112547CLOSED
7AEROLINEAS ARGENTINAS
8AEARGAEROLINEAS ARGENTINASU065467External31105356Closed
9AEARGAEROLINEAS ARGENTINASU066128External31105894Closed
10AEARGAEROLINEAS ARGENTINASU066127External31105893Closed
11AEARGAEROLINEAS ARGENTINASU065904External31105715Closed
12USA JET AIRLINES, INC
13USAJEUSA JET AIRLINES, INCAN20139ExternalR0066612Approved
14USAJEUSA JET AIRLINES, INCAN20436ExternalR0067550Quote
15USAJEUSA JET AIRLINES, INCAN20198ExternalR0066839Closed
16USAJEUSA JET AIRLINES, INCAN20087ExternalR0066419QC
17USAJEUSA JET AIRLINES, INCU065242ExternalR0064547Closed
18PIEDMONT AIRLINES
19USPAIPIEDMONT AIRLINESMN50403External8320095018RClosed
20WERNER AERO SERVICES LLC
21WERNCWERNER AERO SERVICES LLCAN20373-9Internal19088Quote
22WERNCWERNER AERO SERVICES LLCAN20373-11Internal19088QC
Sheet2


I would like the end result to look like this:

Excel 2010
ABCDEFG
1CodeCompany NameWO NumberWO TypeRef NumberTail NumberStatus
2AAR ACS-NY C/O AAR CORP
3AEROLINEAS ARGENTINAS
4USA JET AIRLINES, INC
5PIEDMONT AIRLINES
6WERNER AERO SERVICES LLC
Sheet2
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It is hard to directly group your blank cell, but you can insert a new row and copy the row that contains blank cell, finally delete the oringinal cell, I use an .NET Excel component to perform the codefirst you can vie my oringinal sheet below as the first picture:


use below code:
namespace NETExcelGroup
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"D:\michelle\e-iceblue\Spire.XLS\Demos\Data\DatatableSample.xls");
Worksheet sheet = workbook.Worksheets[0];

sheet.InsertRow(3);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A9:E9"], sheet.Range["A3:E3"], true);
sheet.DeleteRow(9);

sheet.InsertRow(5);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A13:E13"], sheet.Range["A5:E5"], true);
sheet.DeleteRow(13);

sheet.InsertRow(6);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A17:E17"], sheet.Range["A6:E6"], true);
sheet.DeleteRow(17);

sheet.InsertRow(7);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A2:E2"], sheet.Range["A7:E7"], true);
sheet.DeleteRow(2);

//Save the file
workbook.SaveToFile("Sample.xls");

//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
}
}
}
 
Upvote 0
It is hard to directly group your blank cell, but you can insert a new row and copy the row that contains blank cell, finally delete the oringinal cell, I use an .NET Excel component to perform the codefirst you can vie my oringinal sheet below as the first picture:


use below code:
namespace NETExcelGroup
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"D:\michelle\e-iceblue\Spire.XLS\Demos\Data\DatatableSample.xls");
Worksheet sheet = workbook.Worksheets[0];

sheet.InsertRow(3);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A9:E9"], sheet.Range["A3:E3"], true);
sheet.DeleteRow(9);

sheet.InsertRow(5);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A13:E13"], sheet.Range["A5:E5"], true);
sheet.DeleteRow(13);

sheet.InsertRow(6);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A17:E17"], sheet.Range["A6:E6"], true);
sheet.DeleteRow(17);

sheet.InsertRow(7);
sheet.GridLinesVisible = true;
sheet.Copy(sheet.Range["A2:E2"], sheet.Range["A7:E7"], true);
sheet.DeleteRow(2);

//Save the file
workbook.SaveToFile("Sample.xls");

//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
}
}
}


I tried to view your sheet and work with your code but I was unsuccessful. I am not familiar with .net and am just a novice at excel so this is probably why I was unable to get the results I wanted. Thank you for taking the time to look this problem over for me. I appreciate your work. I was able to come up with a solution through other means.

I found this code on an old thread that is a little simpler that I was able to get to work:

Code:
Sub GroupData()

Dim i As Integer, LastRow As Integer
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 3 To LastRow
    If Not Left(Cells(i, 2), 3) = "" Then
        Cells(i, 2).EntireRow.Group
    End If
Next i
End Sub

Now that I have been able to group all of my data. How do I collapse all groups without having to manually do it?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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