Conditional Copy and paste

noobstarrr

New Member
Joined
May 6, 2019
Messages
3
Hello, I have a sheet of data where column one is a list of names and column two is a list of dates. Some names have multiple dates (multiple entries). I need help writing a macro that copies and pastes the rows of data onto a new sheets and if a name is repeated the rows are pasted together on a single line. Here is an example of what I mean:


NameDatevar1var2var3
Jim1/2/18abc
Jim
6/2/18
<strike></strike>
def
Jim
12/2/18
<strike></strike>
ghi
Bob
5/2/18
<strike></strike>
jkl
Kelly
2/2/18
<strike></strike>
mno
Kelly
3/2/18
<strike></strike>
pqr

<tbody>
</tbody>


Would yield:

Name
<strike></strike>
<strike></strike>
Date.1
<strike></strike>
<strike></strike>
var1.1
<strike></strike>
<strike></strike>
var2.1
<strike></strike>
var3.1
<strike></strike>
date.2<strike></strike>
var1.2
<strike></strike>
var2.2
<strike></strike>
<strike></strike>
var3.2
date.3<strike></strike>
var1.3
<strike></strike>
var2.3
<strike></strike>
<strike></strike>
var3.3
<strike></strike>
Jim
1/2/18
<strike></strike>
abc
6/2/18
<strike></strike>
def
12/2/18
<strike></strike>
ghi
Bob
5/2/18
<strike></strike>
jkl
Kelley
3/2/18
<strike></strike>
mno
3/2/18
<strike></strike>
pqr

<tbody>
</tbody>

None of the names have more than 3 entries. I have searched high and low and don't have enough experience myself to write the macro so any help is much appreciated!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Are they always sorted in name order, so all records that need to be combined for a person appear together, like shown in your example?
Or might the names be scattered throughout the list (so maybe Jim's records appear on rows 2, 5, and 10)?
 
Upvote 0
Hi there. Try this code - it assumes no data beyond column E and that all your records for a given name are consecutive. If not, you could sort it first.
Code:
Sub Macro5()
'
' This code assumes data starts in A2 and there is no useful data to the right of column E
'

'
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
    For thisrow = lrow To 2 Step -1
    If Range("a" & thisrow).Text = Range("a" & thisrow - 1).Text Then
    
       Range("f" + Format(thisrow - 1) + ":M" + Format(thisrow - 1)).Value = Range("B" + Format(thisrow) + ":I" + Format(thisrow)).Value
    
       Rows(thisrow).Delete Shift:=xlUp
    End If
    Next thisrow
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,607
Members
449,090
Latest member
vivek chauhan

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