Merge multiple groups of adjacent cells, which are separated by a blank cell, into respective single cells.

NateZona

New Member
Joined
Dec 15, 2015
Messages
16
Dear fine people of Mr. Excel,

I am not sure if this has a possible solution, but here is my problem...

I have a pdf with a large list of hyperlinks which have a text description attached to them. I tried many different ways of copy and paste to get them formatted correctly in excel to no avail.
However, I was able to separate them into groups of cells with a blank cell in between. Some groups are three cells, some are two cells.

Here is an example of what I mean...

Data1a
Data1b
Data1c
Data2a
Data2b
Data3a
Data3b
Data3c
Data4a
Data4b
Data5a
Data5b
Data6a
Data6b

<tbody>
</tbody>

What I would like to do is be able to combine each group into one cell respectively (i.e., "Data1a-c" in one cell, and "Data2a-b" in one cell, etc) using a macro on the whole sheet.


I have so many of these groups that it will take far too long to manually combine them one by one.

Thank you for your time, and I hope to hear from you soon!

Warm Regards,
Nate Zona
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
NateZona,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


I was not sure about what the results should look like. If the following is not correct, then I can adjust the macro.

Sample raw data, and, results:


Excel 2007
ABC
1Data1aData1a, Data1b, Data1c
2Data1bData2a, Data2b
3Data1cData3a, Data3b, Data3c
4Data4a, Data4b
5Data2aData5a, Data5b
6Data2bData6a, Data6b
7
8Data3a
9Data3b
10Data3c
11
12Data4a
13Data4b
14
15Data5a
16Data5b
17
18Data6a
19Data6b
20
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReorgDataGroups()
' hiker95, 12/19/2015, ME909774
Dim o As Variant, j As Long
Dim Area As Range, s As String
Dim lr As Long, sr As Long, er As Long
Application.ScreenUpdating = False
s = ", "
lr = Cells(Rows.Count, 1).End(xlUp).Row
ReDim o(1 To lr, 1 To 1)
For Each Area In Range("A1:A" & lr).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    j = j + 1
    o(j, 1) = Join(Application.Transpose(Range("A" & sr & ":A" & er)), s)
  End With
Next Area
Range("C1").Resize(UBound(o, 1), UBound(o, 2)) = o
Columns(3).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgDataGroups macro.
 
Upvote 0
NateZona,

Here is an updated macro for you to consider.

Sample raw data, and, new results:


Excel 2007
ABC
1Data1aData1a-c
2Data1bData2a-b
3Data1cData3a-c
4Data4a-b
5Data2aData5a-b
6Data2bData6a-b
7
8Data3a
9Data3b
10Data3c
11
12Data4a
13Data4b
14
15Data5a
16Data5b
17
18Data6a
19Data6b
20
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReorgDataGroups_V2()
' hiker95, 12/19/2015, ME909774
Dim o As Variant, j As Long
Dim Area As Range, s As String
Dim lr As Long, sr As Long, er As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
ReDim o(1 To lr, 1 To 1)
For Each Area In Range("A1:A" & lr).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    If .Rows.Count = 1 Then
      j = j + 1
      o(j, 1) = .Range("A" & sr)
    Else
      j = j + 1
      s = ""
      s = s & Range("A" & sr) & "-"
      s = s & Right(Range("A" & er), 1)
      o(j, 1) = s
    End If
  End With
Next Area
Range("C1").Resize(UBound(o, 1), UBound(o, 2)) = o
Columns(3).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgDataGroups_V2 macro.
 
Upvote 0
.. and another one. Mine assumes data starts in cell A2. Adjust A2 to A1 (& possibly B2 to B1) in the code if that suits better.
Rich (BB code):
Sub CombineThem()
  Range("B2").Resize(Columns("A").SpecialCells(xlConstants).Areas.Count).Value = _
    Application.Transpose(Split(Join(Application.Transpose(Range("A2", Range("A" & Rows.Count).End(xlUp)).Value), ", "), ", , "))
End Sub

Original data In Col A, results in Col B

Excel Workbook
AB
1
2Data1aData1a, Data1b, Data1c
3Data1bData2a, Data2b
4Data1cData3a, Data3b, Data3c
5Data4a, Data4b
6Data2aData5a, Data5b
7Data2bData6a, Data6b
8
9Data3a
10Data3b
11Data3c
12
13Data4a
14Data4b
15
16Data5a
17Data5b
18
19Data6a
20Data6b
21
Sheet1
 
Upvote 0
[h=2]
icon1.png
Re: Merge multiple groups of adjacent cells, which are separated by a blank cell, into respective single cells.[/h]
.. and another one. Mine assumes data starts in cell A2. Adjust A2 to A1 (& possibly B2 to B1) in the code if that suits better.
Code:
Sub CombineThem()
Range("B2").Resize(Columns("A").SpecialCells(xlConstants).Areas.Count).Value = _
Application.Transpose(Split(Join(Application.Transpose(Range("A2", Range("A" & Rows.Count).End(xlUp)).Value), ", "), ", , "))
End Sub
Original data In Col A, results in Col B

Sheet1

AB
1
2Data1aData1a, Data1b, Data1c
3Data1bData2a, Data2b
4Data1cData3a, Data3b, Data3c
5 Data4a, Data4b
6Data2aData5a, Data5b
7Data2bData6a, Data6b
8
9Data3a
10Data3b
11Data3c
12
13Data4a
14Data4b
15
16Data5a
17Data5b
18
19Data6a
20Data6b
21


<colgroup><col style="font-weight: bold; width: 30px;"><col style="width: 68px;"><col style="width: 171px;"></colgroup><tbody>
</tbody>


Thank you so much this works perfectly!!

You rock!
 
Upvote 0
NateZona,

I was wondering if you even tried either of my two macros from my replies #2, or, #3?
 
Upvote 0
I did, but they did not work correctly. Thank you though.

NateZona,

You are welcome.

In the future when asking for help I would suggest that you provide a screenshot of the actual raw data, and, what the results should look like, as you did in your reply #6.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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