Combining cells if adjacent cells empty until non-empty cell reached

bookworm121

New Member
Joined
Jun 22, 2011
Messages
39
Hi all!

I have 2 columns, C and D.

Basically, column D contains fragments of a sentence. I need to combine these fragments into one cell. The indication of where the beggining and end of the sentence is in column C. When the adjacent cell in column C is NOT empty, this means the sentence starts here. The sentence will continue until the previous adjacent cell from column C is once again NOT empty (non-inclusive). The sentence is built backwards.

For example: (where "nothing" is written indicates a blank cell, where a number is written indicates a non empty cell)

C D
0 random.
nothing The.
nothing sky
nothing is
nothing very
1 Blue
nothing He

In this example, the sentence is "The sky is very blue.". A illustrated, when column C is not blank, the sentence starts, and works backwards until the next non-empty C cell (non-inclusive).

The words "He" and "random." are the beggining and end of 2 different sentences.

I need a macro that would be able to do this on a spreadsheet of any size. This means columns C and D may be 100 rows, 2000 rows, 2 rows or any random number of rows.

I was thinking I need to use "&" somehow, but i really can't seem to wrap my head around how to do this.

Any help would be appreciated! thanks!
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi bookworm121,

Try this:
Code:
Sub Combine_Cells()
'Done by César C., 22/06/2011

Dim SentencesRow(), LastRow As Integer, NotEmptyRows As Integer
Dim i As Integer, j As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False

LastRow = Range("C" & Rows.Count).End(xlUp).Row
NotEmptyRows = WorksheetFunction.CountA(Range("C1:C" & LastRow))

If NotEmptyRows > 0 Then
    ReDim SentencesRow(1 To NotEmptyRows)
    'Loading array of row position
    j = 1
    For i = 1 To LastRow
        If Not IsEmpty(Cells(i, "C")) Then
            SentencesRow(j) = i
            j = j + 1
        End If
    Next
    'Modifying values in column D before to combine
    For i = 1 To UBound(SentencesRow) - 1
        For j = (SentencesRow(i) + 1) To SentencesRow(i + 1) - 1
            Cells(SentencesRow(i) + 1, "D") = Cells(SentencesRow(i) + 1, "D") & " " & Cells(j + 1, "D")
        Next
    Next
    'Combining cells by sentence
    For i = 1 To UBound(SentencesRow) - 1
        With Range("D" & SentencesRow(i) + 1 & ":D" & SentencesRow(i + 1))
            .Merge
            .HorizontalAlignment = xlGeneral
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlTop
            .WrapText = True
        End With
    Next
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Hope this helps.

Regards
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,323
Members
449,154
Latest member
pollardxlsm

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