create sheets based on column and copy data without repeating

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
hello
I have data in sheet1 contains data from a1: f and names ae existed in column b so I want creating sheets based on column b and copy data each name without repeating
1 .xlsm
ABCDEF
1datecustomer nameclient numberinvoice nodebitcredit
201/01/2020aliasd1223IN10001500001000
302/01/2020omarasd1224IN1001580000
403/01/2020mohammedasd1225IN10021700007000
504/01/2020mariamasd1226IN1003600000100
605/01/2020sarahasd1227IN1004453200400
706/01/2020mahmoudasd1228IN100515000010000
807/01/2020aliasd1223IN100050000
908/01/2020omarasd1224IN10011000580000
1009/01/2020mohammedasd1225IN1002100000
1110/01/2020mariamasd1226IN1003500000
1211/01/2020sarahasd1227IN1004435000
1312/01/2020mahmoudasd1228IN1005100000
1413/01/2020sarahasd1227IN100623000
1514/01/2020mahmoudasd1228IN10075000
1615/01/2020omarasd1224IN10011000
1716/01/2020khalidasd12299in10032500005000
1817/01/2020khalidasd12300in100335000
sheet1

thanks in advance
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this:
VBA Code:
Sub GetUniques()

Dim d As Object, c As Variant, i As Long, lr As Long, ws As Worksheet, lr2 As Long, WS2 As Worksheet
Set ws = Sheets("Sheet1")
Set d = CreateObject("Scripting.Dictionary")
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
c = Range("B2:B" & lr)
For i = 1 To UBound(c, 1)
  d(c(i, 1)) = 1
Next i
ws.Range("H2").Resize(d.Count) = Application.Transpose(d.keys)
lr2 = ws.Cells(Rows.Count, 8).End(xlUp).Row
For Each Cell In ws.Range("H2:H" & lr2)
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = Cell.Value
Next Cell

For Each Worksheet In Worksheets
Set WS2 = Sheets(Worksheet.Name)
If WS2.Name <> "Sheet1" Then
For j = 1 To 6
WS2.Cells(1, j).Value = ws.Cells(1, j).Value
For i = 2 To lr
If ws.Cells(i, 2).Value = WS2.Name Then
WS2.Cells(i, j).Value = ws.Cells(i, j).Value
End If
Next i
Next j
End If
For i = lr To 2 Step -1
If WS2.Range("A" & i).Value = "" Then
WS2.Range("A" & i).EntireRow.Delete
End If
Next i
Next Worksheet
ws.Range("H2:H" & lr2).ClearContents
End Sub
 
Upvote 0
wow ! great work but there is still something needs fixing if I run the macro repeatedly it gives me error the aim of run continuously may be I add a new data in sheet1 then if the sheet is already created then should update data i mean add the new data to old data I have ever copied
 
Upvote 0
Hi,
try following & see if helps

VBA Code:
Option Explicit
Sub FilterColumn()
    'dmt32 aug 2020
    Dim wsData As Worksheet, wsNames As Worksheet, wsFilter As Worksheet
    Dim Datarng As Range, FilterRange As Range
    Dim rowcount As Long
    Dim FilterCol As Variant
    Dim SheetName As String
   
    On Error GoTo progend
'your master sheet
    Set wsData = ThisWorkbook.Worksheets("Sheet1")
   
'Column you are filtering
    FilterCol = "B"
   
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
'add filter sheet
    Set wsFilter = ThisWorkbook.Worksheets.Add
   
    With wsData
        .Activate
'add password if needed
        .Unprotect Password:=""
       
        Set Datarng = .Range("A1").CurrentRegion
       
'extract values from FilterCol'to filter sheet
        .Cells(1, FilterCol).Resize(Datarng.Rows.Count).AdvancedFilter Action:=xlFilterCopy, _
        CopyToRange:=wsFilter.Range("A1"), Unique:=True
       
        rowcount = wsFilter.Cells(wsFilter.Rows.Count, "A").End(xlUp).Row
'set Criteria
        wsFilter.Range("B1").Value = wsFilter.Range("A1").Value
       
        For Each FilterRange In wsFilter.Range("A2:A" & rowcount)
            SheetName = CStr(Left(FilterRange.Text, 31))
'check for blank cell in range
            If SheetName <> "" Then
'add the FilterRange to criteria
'part matches
                'wsFilter.Range("B2").Value = FilterRange.Value
'exact matches only
                wsFilter.Range("B2").Formula = "=" & """=" & SheetName & """"
'check if sheet exists
                If Not Evaluate("ISREF('" & SheetName & "'!A1)") Then
'add new sheet
                    Worksheets.Add(After:=Sheets(Sheets.Count)).Name = SheetName
                End If
'set object variable to sheet
                Set wsNames = Worksheets(SheetName)
'clear sheet
                wsNames.UsedRange.Clear
'copy data
                Datarng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wsFilter.Range("B1:B2"), _
                CopyToRange:=wsNames.Range("A1"), Unique:=False
            End If

'size column widths to match master
'NB - slows code down a little
            Datarng.Rows(1).Copy
            wsNames.UsedRange.Rows(1).PasteSpecial xlPasteColumnWidths
'clear from memory
            Set wsNames = Nothing
'clear clipboard
            Application.CutCopyMode = False
        Next
        .Select
    End With
   
progend:
    If Not wsFilter Is Nothing Then wsFilter.Delete
    With Application
        .ScreenUpdating = True: .DisplayAlerts = True
    End With
   
    If Err <> 0 Then MsgBox (Error(Err)), vbCritical, "Error"
       
End Sub

Code will, if it does not already exist, create sheet for each name in column B & copy all matching data to it - You can refresh data as often as required

Dave
 
Upvote 0
Solution
@ Dmt32
brilliant ! despite of the huge code , but it works very well , this is what I look for .
many thanks ! (y)
 
Upvote 0
@ Dmt32
brilliant ! despite of the huge code , but it works very well , this is what I look for .
many thanks ! (y)

It only looks "huge" because I include line comments & error handling

Glad it works ok for you

Dave
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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