VBA to Copy Unique Values to New Sheet

srj1359

New Member
Joined
Mar 5, 2015
Messages
46
Office Version
  1. 2016
Platform
  1. Windows
Hello!

I've got a "Quote Summary" sheet that I need to copy over data to a "Title List" sheet. How can I create a VBA that will copy each group of customers to the title sheet. Screenshot is attached. For example, I want just Customer A on the title list sheet, then just Customer B on a separate title list sheet. Any assistance you can provide is greatly appreciated.

Thank you!
 

Attachments

  • example.PNG
    example.PNG
    49.1 KB · Views: 28

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You can have only one sheet in the workbook named "Title List". If you want each unique customer on a separate sheet then the sheet names would have to have a suffix like A, B, C or 1, 2, 3, etc.
The code can create and name the sheets. If you want to do it that way then this code would do that.

VBA Code:
Sub UniqueCust()
Dim sh As Worksheet, nsh As Worksheet, lr As Long, c As Range, x As Long
Set sh = ActiveSheet
lr = sh.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
sh.Range("A1", sh.Cells(Rows.Count, 1).End(xlUp)).AdvancedFilter xlFilterCopy, , sh.Cells(lr + 2, 2), True
    For Each c In sh.Cells(Rows.Count, 2).End(xlUp).CurrentRegion.Offset(1)
        If c <> "" Then
            Set nsh = Sheets.Add(After:=Sheets(Sheets.Count))
            x = x + 1
            nsh.Name = "Title List" & x
            sh.UsedRange.AutoFilter 1, c.Value
            sh.UsedRange.Copy nsh.Range("A1")
            sh.AutoFilterMode = False
            Set nsh = Nothing
        End If
    Next
sh.Cells(Rows.Count, 2).End(xlUp).CurrentRegion.ClearContents
End Sub
 
Upvote 0
Hi @JLGWhiz ,

Thank you for your response! Unfortunately, it's not doing anything for me. I'm not getting an error message, but it's also not generating anything like I would expect.
 
Upvote 0
this is my test set up
Discussions.xlsm
ABC
1hhh
21231c1
31232c1
41233c1
52344c2
62345c2
72346c2
82347c2
93458c3
103459c3
Sheet1


The code produced three new sheets name Title List1, Title List2 and Title List3 with the appropriate data listed on each sheet. If your data is not in columns 1:3 then the code fails. If you run the code when a different sheet than the one with the three columns of data is active, the code fails.

Since sheet names were not provided, the sh variable was used to identify the sheet with the source data as the ActiveSheet. If any other sheet is active at run time, the the sh variable will be referring to that sheet instead of the one with the source data.
 
Upvote 0
HI
Another approach
your data is not in columns 1:3
VBA Code:
Sub test()
    Dim a As Variant, i, s, k, ky, itm, c
    Dim nsh As Worksheet
    a = Sheets("sheet1").Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 3)
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If a(i, 1) <> "" Then
                If Not .exists(a(i, 3)) Then
                    .Add a(i, 3), a(i, 1) & "," & a(i, 2) & "-"
                Else
                    .Item(a(i, 3)) = .Item(a(i, 3)) & a(i, 1) & "," & a(i, 2) & "-"
                End If: End If
        Next
        itm = .items:  ky = .keys
        c = 0
        For Each k In .keys
            Set nsh = Sheets.Add(After:=Sheets(Sheets.Count))
            nsh.Name = k
            s = Split(itm(c), "-")
            With Cells(1, 1)
            .Resize(UBound(s)) = Application.Transpose(s)
            .Resize(UBound(s)).TextToColumns , , , , 1, , , , 1
            .Offset(, 2).Resize(UBound(s)) = Application.Transpose(ky(c))
           End With
            c = c + 1
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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