Sub CopyDupesV4()
' hiker95, 07/07/2015, ME865799
Dim wr As Worksheet, w1 As Worksheet, w2 As Worksheet, w3 As Worksheet
Dim w4 As Worksheet, w5 As Worksheet, w6 As Worksheet
Dim a As Variant, r As Long, lr As Long, lc As Long, n As Long, nr As Long, lr2 As Long
Application.ScreenUpdating = False
Set wr = Sheets("Raw_Data")
Set w1 = Sheets("1_Referral")
Set w2 = Sheets("2_Referral")
Set w3 = Sheets("3_Referral")
Set w4 = Sheets("4_Referral")
Set w5 = Sheets("5_Referral")
Set w6 = Sheets("6_Referral")
w1.UsedRange.Clear
w2.UsedRange.Clear
w3.UsedRange.Clear
w4.UsedRange.Clear
w5.UsedRange.Clear
w6.UsedRange.Clear
With wr
.Activate
lr = .Cells(Rows.Count, 1).End(xlUp).Row
lc = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w1.Cells(1, 1)
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w2.Cells(1, 1)
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w3.Cells(1, 1)
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w4.Cells(1, 1)
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w5.Cells(1, 1)
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w6.Cells(1, 1)
Application.CutCopyMode = False
a = .Range(.Cells(1, 1), .Cells(lr, lc))
.Range(.Cells(2, 1), .Cells(lr, lc)).Sort key1:=.Range("A2"), order1:=1, key2:=.Range("I2"), order2:=2
For r = 2 To lr
n = Application.CountIf(.Columns(1), .Cells(r, 1).Value)
If n = 1 Then
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w1.Cells(1, 1)
nr = w1.Cells(w1.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r, lc)).Copy w1.Cells(nr, 1)
Application.CutCopyMode = False
ElseIf n = 2 Then
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w2.Cells(1, 1)
nr = w2.Cells(w2.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w2.Cells(nr, 1)
Application.CutCopyMode = False
ElseIf n = 3 Then
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w3.Cells(1, 1)
nr = w3.Cells(w3.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w3.Cells(nr, 1)
Application.CutCopyMode = False
ElseIf n = 4 Then
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w4.Cells(1, 1)
nr = w4.Cells(w4.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w4.Cells(nr, 1)
Application.CutCopyMode = False
ElseIf n = 5 Then
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w5.Cells(1, 1)
nr = w5.Cells(w5.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w5.Cells(nr, 1)
Application.CutCopyMode = False
ElseIf n > 5 Then
.Range(.Cells(1, 1), .Cells(1, lc)).Copy w6.Cells(1, 1)
nr = w6.Cells(w6.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w6.Cells(nr, 1)
Application.CutCopyMode = False
End If
r = r + n - 1
Next r
'***** new section for REFERREDBY, column K = 11
.Range(.Cells(2, 1), .Cells(lr, lc)).Sort key1:=.Range("K2"), order1:=2
lr2 = .Cells(Rows.Count, "K").End(xlUp).Row
.Range(.Cells(2, 1), .Cells(lr2, lc)).Sort key1:=.Range("K2"), order1:=1
For r = 2 To lr2
n = Application.CountIf(.Columns(11), .Cells(r, 11).Value)
If n = 1 Then
nr = w1.Cells(w1.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r, lc)).Copy w1.Cells(nr, 1)
Application.CutCopyMode = False
w1.Cells(nr, 1).Resize(n).Interior.Color = vbYellow
ElseIf n = 2 Then
nr = w2.Cells(w2.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w2.Cells(nr, 1)
Application.CutCopyMode = False
w2.Cells(nr, 1).Resize(n).Interior.Color = vbYellow
ElseIf n = 3 Then
nr = w3.Cells(w3.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w3.Cells(nr, 1)
Application.CutCopyMode = False
w3.Cells(nr, 1).Resize(n).Interior.Color = vbYellow
ElseIf n = 4 Then
nr = w4.Cells(w4.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w4.Cells(nr, 1)
Application.CutCopyMode = False
w4.Cells(nr, 1).Resize(n).Interior.Color = vbYellow
ElseIf n = 5 Then
nr = w5.Cells(w5.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w5.Cells(nr, 1)
Application.CutCopyMode = False
w5.Cells(nr, 1).Resize(n).Interior.Color = vbYellow
ElseIf n > 5 Then
nr = w6.Cells(w6.Rows.Count, "A").End(xlUp).Row + 1
.Range(.Cells(r, 1), .Cells(r + n - 1, lc)).Copy w6.Cells(nr, 1)
Application.CutCopyMode = False
w6.Cells(nr, 1).Resize(n).Interior.Color = vbYellow
End If
r = r + n - 1
Next r
'***** write the a array back to its starting point
.Range(.Cells(1, 1), .Cells(lr, lc)) = a
Erase a
End With
w1.Columns(1).Resize(, lc).AutoFit
w2.Columns(1).Resize(, lc).AutoFit
w3.Columns(1).Resize(, lc).AutoFit
w4.Columns(1).Resize(, lc).AutoFit
w5.Columns(1).Resize(, lc).AutoFit
w6.Columns(1).Resize(, lc).AutoFit
Application.ScreenUpdating = True
End Sub