VBA - loop thru column, find 2 consecutive empty rows, combine data in row below thru n rows before next 2 consecutive empty rows

gratefuljames

New Member
Joined
Nov 2, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Greetings,

I would like to get help to create a loop in VBA that will do the following in Excel Office 365:

- loop down entire column and find 2 consecutive empty rows in column A
- combine data from last empty row +2 rows down thru last row with data before the next 2 consecutive empty rows, including any empty row, in the same layout
- paste the combined data into column B, on the row where the 1st row with data appears for each block between consecutive empty rows
- delete empty rows in column B

Below is a sample data:
excel 1.jpg


Below is what it should look like after combining the data and pasting
excel 2.jpg


Below is after deletion of empty rows in column B
excel 3.jpg



I appreciate any help.

Thanks,
James
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,796
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub gratefuljames()
   Dim i As Long
   Dim Ar As Areas
   Dim Rng As Range
   
   Set Ar = Range("A:A").SpecialCells(xlBlanks).Areas
   For i = 1 To Ar.Count
      If Ar(i).Count = 2 Then
         If Rng Is Nothing Then
            Set Rng = Ar(i).Offset(3)(1)
         Else
            Rng.Offset(-1, 1).Value = Join(Application.Transpose(Range(Rng, Ar(i).Offset(-1)(1))), vbLf)
            Set Rng = Ar(i).Offset(3)(1)
         End If
      End If
   Next i
   Rng.Offset(-1, 1).Value = Join(Application.Transpose(Range(Rng, Range("A" & Rows.Count).End(xlUp))), vbLf)
   Range(Range("B1").End(xlDown), Range("B" & Rows.Count)).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Solution

gratefuljames

New Member
Joined
Nov 2, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi Fluff, your solution works perfectly for my requirement. Thank you for the quick response.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,796
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

gratefuljames

New Member
Joined
Nov 2, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

hi Fluff,

I have a data set in which the consecutive empty rows vary from 2 to 3 rows. Is there a way to account for this?

Thanks,
James
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,796
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub gratefuljames()
   Dim i As Long
   Dim Ar As Areas
   Dim Rng As Range
   
   Set Ar = Range("A:A").SpecialCells(xlBlanks).Areas
   For i = 1 To Ar.Count
      If Ar(i).Count >= 2 Then
         If Rng Is Nothing Then
            Set Rng = Ar(i).Offset(Ar(i).Count + 1)(1)
         Else
            Rng.Offset(-1, 1).Value = Join(Application.Transpose(Range(Rng, Ar(i).Offset(-1)(1))), vbLf)
            Set Rng = Ar(i).Offset(Ar(i).Count + 1)(1)
         End If
      End If
   Next i
   Rng.Offset(-1, 1).Value = Join(Application.Transpose(Range(Rng, Range("A" & Rows.Count).End(xlUp))), vbLf)
   Range(Range("B1").End(xlDown), Range("B" & Rows.Count)).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 

gratefuljames

New Member
Joined
Nov 2, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi Fluff,

It works as expected. Thank you for taking the time to make the updates.

James
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,796
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,153
Messages
5,629,000
Members
416,358
Latest member
grsaltzman

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
Top