B
B
blainethemono2015-06-08 15:43:01
excel
blainethemono, 2015-06-08 15:43:01

How to merge excel cells by condition?

Hello.
In the first column of excel, the values ​​corresponding to them in the second, but in different lines:
96f16ab77e934baca5af511debb9196e.PNG
You need to combine all "a" in a cell opposite 1, all b opposite 2, etc., while preserving line wrapping:
1464c505a48d4646abb611fc7c2e9e28.PNG
Naturally, there are thousands of records.
Tell me, please, how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tplus, 2015-06-08
@blainethemono

I threw it without any special beauty :)
Of the minuses:
- between the values ​​after processing there are empty lines equal to the number of elements - 1 in the second column (respectively concatenated values); empty lines in excel should not be a problem;
- the initial initialization is tied to the location of the elements.
Don't forget to extend the Range() for the loop.

Sub concat()
    txt = Range("B1").Text
    Range("A1").Select
    Set hCell = ActiveCell
    For Each cell In Range("A1:A20")
        If cell.Value <> "" Or cell.Offset(0, 1).Value = "" Then
            hCell.Offset(0, 1).Value = txt
            cell.Select
            Set hCell = ActiveCell
            txt = ""
        End If
        
        If cell.Offset(0, 1).Value = "" Then
            Exit Sub
        End If
        
        txt = txt + vbCrLf + cell.Offset(0, 1).Text
        ' Очистка ячейки
        cell.Offset(0, 1).Value = ""
    Next
End Sub

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question