How to copy all the unique values to another excel sheet using macro?

Rampage598

New Member
Joined
Mar 11, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
pincodevendorratingpending cases
1xyz20
2xyz241
3xyz245
1xyz14
2xyz317
4xyz20
5xyz245
6xyz317

This is my excel data i want to copy all the unique data of pincode to another excel sheet..
For eg. here there are pincode "1" which has 2count then all the data of pincode 1 will get copy into the another sheet with the name of pincode "1.xlsx" will create the file then it will copy all the data for 1 with headers into that created excel sheet..

How to do this in macro? i dont have any idea
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Maybe:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
    Dim unq As Variant, x As Long
    Dim wb As Workbook, ws As Worksheet
    
    Set rng = Range("A1").CurrentRegion
    unq = Application.Sort(Application.Unique(rng.Offset(1).Resize(rng.Rows.Count - 1, 1)))
    
    rng.AutoFilter
    For x = 1 To UBound(unq)
        rng.AutoFilter 1, unq(x, 1)
        Set wb = Workbooks.Add
        Set ws = wb.Sheets(1)
        rng.SpecialCells(xlCellTypeVisible).Copy
        ws.Range("A1").PasteSpecial (xlValues)
        Application.CutCopyMode = False
        ws.Cells.EntireColumn.AutoFit
        ws.Rows(1).Font.Bold = True
        wb.SaveAs ThisWorkbook.Path & "\" & x & ".xlsx"
        wb.Close False
    Next x
    rng.AutoFilter
End Sub
 
Upvote 0
Maybe:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
    Dim unq As Variant, x As Long
    Dim wb As Workbook, ws As Worksheet
   
    Set rng = Range("A1").CurrentRegion
    unq = Application.Sort(Application.Unique(rng.Offset(1).Resize(rng.Rows.Count - 1, 1)))
   
    rng.AutoFilter
    For x = 1 To UBound(unq)
        rng.AutoFilter 1, unq(x, 1)
        Set wb = Workbooks.Add
        Set ws = wb.Sheets(1)
        rng.SpecialCells(xlCellTypeVisible).Copy
        ws.Range("A1").PasteSpecial (xlValues)
        Application.CutCopyMode = False
        ws.Cells.EntireColumn.AutoFit
        ws.Rows(1).Font.Bold = True
        wb.SaveAs ThisWorkbook.Path & "\" & x & ".xlsx"
        wb.Close False
    Next x
    rng.AutoFilter
End Sub
its working but i want to save as the pincode name.... if i have pincode "4560003" then it will save as this name
 
Upvote 0
Change this:
VBA Code:
wb.SaveAs ThisWorkbook.Path & "\" & x & ".xlsx"
To:
VBA Code:
wb.SaveAs ThisWorkbook.Path & "\" & unq(x, 1) & ".xlsx"
 
Upvote 0
Change this:
VBA Code:
wb.SaveAs ThisWorkbook.Path & "\" & x & ".xlsx"
To:
VBA Code:
wb.SaveAs ThisWorkbook.Path & "\" & unq(x, 1) & ".xlsx"
Hey, can you give me the code for this data?
namescoreservicevendor
1​
asdaasdSamsung
2​
asdaasdSamsung
3​
asdaasdSamsung
4​
asdaasdApple
5​
asdaasdApple
6​
asdaasdApple
7​
asdaasdNokia
8​
asdaasdNokia
9​
asdaasdNokia
10​
asdaasdNokia
11​
asdaasdVivo
12​
asdaasdVivo
13​
asdaasdVivo
14​
asdaasdOppo
15​
asdaasdVivo
16​
asdaasdVivo
17​
asdaasdNokia
18​
asdaasdOppo

Now i want to create the files according to the vendor column.... vendor column unique data need to transfer into the to separate excel sheet
 
Upvote 0
Maybe:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
    Dim unq As Variant, x As Long
    Dim wb As Workbook, ws As Worksheet
    
    Set rng = Range("A1").CurrentRegion
    unq = Application.Sort(Application.Unique(rng.Offset(1, 3).Resize(rng.Rows.Count - 1, 1)))
    
    rng.AutoFilter
    For x = 1 To UBound(unq)
        rng.AutoFilter 4, unq(x, 1)
        Set wb = Workbooks.Add
        Set ws = wb.Sheets(1)
        rng.SpecialCells(xlCellTypeVisible).Copy
        ws.Range("A1").PasteSpecial (xlValues)
        Application.CutCopyMode = False
        ws.Cells.EntireColumn.AutoFit
        ws.Rows(1).Font.Bold = True
        wb.SaveAs ThisWorkbook.Path & "\" & unq(x, 1) & ".xlsx"
        wb.Close False
    Next x
    rng.AutoFilter
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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