Merge multiple rows into one

appletree

New Member
Joined
Oct 10, 2016
Messages
4
Help me fix my contact list! I want each person information into one row.
Sometime they have 6rows, mostly 4 like below.
Tried doing this manually, but it will take me months, please help me friends.

Problem:
John Doe
Oakhill
12345 Ny
Karl Doe
Appleroad
12345 Jersey
Karla Doe
Orangeroad
34444 Ny
Richard Doe
Hilltop
55555 Boston

<tbody>
</tbody>

How i want
John Doe, Oakhill, 12345 Ny, 11233, Customer, Yes
Karl Doe, Appleroad, 12345 Jersey, 11111,Customer, No
Karla Doe, Orangeroad,34444 Ny, 1212, Customer, Yes
Richard Doe, Hilltop ,55555 Boston, 122331, Customer,Yes

<tbody>
</tbody>
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You give far too little information to be able to help.

What column is all the data in?

How is the data separated?

Do ALL contacts have the exact number of lines?

Sample data would help
 
Upvote 0
You give far too little information to be able to help.

What column is all the data in?

How is the data separated?

Do ALL contacts have the exact number of lines?

Sample data would help


Thanks Gallen for the reply,i appreciate it!


I copy & pasted from my contact list to Google sheets and re-named fields as you can see.
Every black line starts new contact.
Link : https://docs.google.com/spreadsheets/d/1o3p48W5Bfv8QWc5SKQNOuOruRcpPwXtdul3W-_9FWBY/edit?usp=sharing




I want to filter contacts for example by location or name in Excel.
Please note:
1) There are 1-3 empty rows before each black line.


I hope this answers to your questions.
 
Upvote 0
Sorry, I can't open links on my work's PC :mad:

If there is at least one blank line then you can work with that.

Is there any possibility of a blank line within a contact's details and not just at the end?
 
Upvote 0
This is how it was on google sheets, thanks for letting me now friend! :D
I was not able to create black bar, neither i am not sure how it is made.
We could fill blanks with something, Column B is not important but would help a lot if we could take it also.

RowColumn AColumn B
1Customer Name, LocationInformation ID
2Text, 2nd LocationDate
3AddressDate
4
5
6
7
8Customer NameInformation ID
9TextDate
10TextDate
11Text
12Location
13Address
14
15
16
17Customer NameInformation ID
18TextDate
19LocationDate
20Address
21
22
23Customer NameInformation ID
24TextDate
25Location
26Address
27
28
29Customer Name, LocationInformation ID
30Text, LocationDate
31TextDate
32Text
33Location
34Address
35
36
37Customer NameInformation ID
38TextDate
39TextDate
40Text, Location
41
42
43Customer NameInformation ID
44LocationDate
45AddressDate
46
47
48
49Customer NameInformation ID
50TextDate
51LocationDate
52Address

<tbody>
</tbody>
 
Upvote 0
OK, This code, is tested using the above as data and works.

This creates the information in to a worksheet named "Results" which you will have to create before running the code

Paste this in to the code window of your sheet with the data and it will work fine. You could also paste it into a module.

Code:
Sub CollateContacts()    
Dim l As Long
Dim lResults As Long 'counter for row in sheet results

Dim wsData As Worksheet 'Sheet with original data
Dim wsResults As Worksheet 'Sheet with formatted data

Dim s As String, sAppend As String

    'Set the 2 worksheets to variables for easy reference CHANGE TO YOUR SHEETS
    Set wsData = ThisWorkbook.Worksheets("Data")
    Set wsResults = ThisWorkbook.Worksheets("Results")
    
    lResults = 2 'First row to paste details
    
    With wsData
        'loop through all rows on data sheet and generate the string
        For l = 2 To .Range("A" & Rows.Count).End(xlUp).Row
            Do Until .Range("A" & l) = ""
                If s = "" Then
                    s = .Range("A" & l)
                Else
                    s = s & ", " & .Range("A" & l)
                End If
                'As the info in column B goes at the end we stroe it in a separate string variable
                sAppend = IIf(Len(.Range("B" & l)) > 0, sAppend & ", " & .Range("B" & l), "")
                
                l = l + 1
            Loop
            'If we have a value in s then we have a row of results to write in to results
            If Len(s) > 0 Then
                wsResults.Range("A" & lResults) = s & sAppend
                lResults = lResults + 1
                s = ""
            End If
        Next
    End With
    
End Sub
 
Last edited:
Upvote 0
I can't believe this! With pressing a button once it solved my problem just like that!

This worked perfectly, Thank you!
 
Upvote 0
Actually I spotted an error in the code :oops:.
I noticed that I didn't clear the sAppend variable which showed that it was being cleared by mistake in the IIF statement.
I've shown the fixes in red:

Code:
Sub CollateContacts()
Dim l As Long
Dim lResults As Long 'counter for row in sheet results


Dim wsData As Worksheet 'Sheet with original data
Dim wsResults As Worksheet 'Sheet with formatted data


Dim s As String, sAppend As String


    'Set the 2 worksheets to variables for easy reference CHANGE TO YOUR SHEETS
    Set wsData = ThisWorkbook.Worksheets("Data")
    Set wsResults = ThisWorkbook.Worksheets("Results")
    
    lResults = 2 'First row to paste details
    
    With wsData
        'loop through all rows on data sheet and generate the string
        For l = 2 To .Range("A" & Rows.Count).End(xlUp).Row
            Do Until .Range("A" & l) = ""
                If s = "" Then
                    s = .Range("A" & l)
                Else
                    s = s & ", " & .Range("A" & l)
                End If
                'As the info in column B goes at the end we stroe it in a separate string variable
                sAppend = IIf(Len(.Range("B" & l)) > 0, sAppend & ", " & .Range("B" & l), [B][COLOR=#ff0000]sAppend[/COLOR][/B])
                
                l = l + 1
            Loop
            'If we have a value in s then we have a row of results to write in to results
            If Len(s) > 0 Then
                wsResults.Range("A" & lResults) = s & sAppend
                lResults = lResults + 1
                s = ""
               [B][COLOR=#ff0000] sAppend = ""[/COLOR][/B]
            End If
        Next
    End With
    
End Sub
I missed it because it did work for the first item in your example data. I should have made the data more real to spot it earlier. It works now. Honest :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,358
Messages
6,136,093
Members
449,991
Latest member
IslandofBDA

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