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...

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Data1a[/TD]
[/TR]
[TR]
[TD]Data1b[/TD]
[/TR]
[TR]
[TD]Data1c[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Data2a[/TD]
[/TR]
[TR]
[TD]Data2b[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Data3a[/TD]
[/TR]
[TR]
[TD]Data3b[/TD]
[/TR]
[TR]
[TD]Data3c[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Data4a[/TD]
[/TR]
[TR]
[TD]Data4b[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Data5a[/TD]
[/TR]
[TR]
[TD]Data5b[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Data6a[/TD]
[/TR]
[TR]
[TD]Data6b[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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
Data1aData1a, Data1b, Data1c
Data1bData2a, Data2b
Data1cData3a, Data3b, Data3c
Data4a, Data4b
Data2aData5a, Data5b
Data2bData6a, Data6b
Data3a
Data3b
Data3c
Data4a
Data4b
Data5a
Data5b
Data6a
Data6b



<colgroup><col style="font-weight: bold; width: 30px;"><col style="width: 68px;"><col style="width: 171px;"></colgroup><tbody>
[TD="bgcolor: #CACACA, align: center"]1[/TD]

[TD="bgcolor: #CACACA, align: center"]2[/TD]

[TD="bgcolor: #CACACA, align: center"]3[/TD]

[TD="bgcolor: #CACACA, align: center"]4[/TD]

[TD="bgcolor: #CACACA, align: center"]5[/TD]

[TD="bgcolor: #CACACA, align: center"]6[/TD]

[TD="bgcolor: #CACACA, align: center"]7[/TD]

[TD="bgcolor: #CACACA, align: center"]8[/TD]

[TD="bgcolor: #CACACA, align: center"]9[/TD]

[TD="bgcolor: #CACACA, align: center"]10[/TD]

[TD="bgcolor: #CACACA, align: center"]11[/TD]

[TD="bgcolor: #CACACA, align: center"]12[/TD]

[TD="bgcolor: #CACACA, align: center"]13[/TD]

[TD="bgcolor: #CACACA, align: center"]14[/TD]

[TD="bgcolor: #CACACA, align: center"]15[/TD]

[TD="bgcolor: #CACACA, align: center"]16[/TD]

[TD="bgcolor: #CACACA, align: center"]17[/TD]

[TD="bgcolor: #CACACA, align: center"]18[/TD]

[TD="bgcolor: #CACACA, align: center"]19[/TD]

[TD="bgcolor: #CACACA, align: center"]20[/TD]

[TD="bgcolor: #CACACA, align: center"]21[/TD]

</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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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