SOLVED

Filter Range Copy Paste the Value and Create new Sheets

%3CLINGO-SUB%20id%3D%22lingo-sub-3000201%22%20slang%3D%22en-US%22%3EFilter%20Range%20Copy%20Paste%20the%20Value%20and%20Create%20new%20Sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3000201%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20been%20trying%20to%20find%20an%20way%20to%20create%20multiple%20sheets%20using%20Specific%20Column%20data.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20Col%22A%22%20has%20multiple%20duplicate%20entries%20then%20filter%20single%20value%20create%20the%20new%20sheet%20using%20that%20value%20name%2C%20copy%20all%20the%20data%20and%20paste%20into%20newly%20added%20sheet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20unable%20to%20elaborate%20this%20thing%20in%20words%20and%20sorry%20for%20my%20poor%20English%2C%20i%20have%20attached%20an%20example%20workbook.%3C%2FP%3E%3CP%3EWhere%20Sheet1%20has%20data%20using%20Column%20A%20code%20will%20create%20multiple%20sheets.%20Your%20help%20will%20be%20much%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22%22%3ESub%3C%2FSPAN%3E%20CopyPartOfFilteredRange()%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3EDim%3C%2FSPAN%3E%20src%20%3CSPAN%20class%3D%22%22%3EAs%3C%2FSPAN%3E%20Worksheet%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3EDim%3C%2FSPAN%3E%20tgt%20%3CSPAN%20class%3D%22%22%3EAs%3C%2FSPAN%3E%20Worksheet%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3EDim%3C%2FSPAN%3E%20filterRange%20%3CSPAN%20class%3D%22%22%3EAs%3C%2FSPAN%3E%20Range%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3EDim%3C%2FSPAN%3E%20copyRange%20%3CSPAN%20class%3D%22%22%3EAs%3C%2FSPAN%3E%20Range%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3EDim%3C%2FSPAN%3E%20lastRow%20%3CSPAN%20class%3D%22%22%3EAs%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22%22%3ELong%3C%2FSPAN%3E%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3ESet%3C%2FSPAN%3E%20src%3DThisWorkbook.Sheets(%3CSPAN%20class%3D%22%22%3E%22Sheet1%22%3C%2FSPAN%3E)%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3ESet%3C%2FSPAN%3E%20tgt%20%3D%20ThisWorkbook.Sheets(%3CSPAN%20class%3D%22%22%3E%22Sheet8%22%3C%2FSPAN%3E)%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20src.AutoFilterMode%20%3D%20%3CSPAN%20class%3D%22%22%3EFalse%3C%2FSPAN%3E%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20lastRow%20%3D%20src.Range(%3CSPAN%20class%3D%22%22%3E%22A%22%3C%2FSPAN%3E%20%26amp%3B%20src.Rows.Count).%3CSPAN%20class%3D%22%22%3EEnd%3C%2FSPAN%3E(xlUp).Row%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3ESet%3C%2FSPAN%3E%20filterRange%20%3D%20src.Range(%3CSPAN%20class%3D%22%22%3E%22A1%3AA%22%3C%2FSPAN%3E%20%26amp%3B%20lastRow)%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22%22%3ESet%3C%2FSPAN%3E%20copyRange%20%3D%20src.Range(%3CSPAN%20class%3D%22%22%3E%22A1%3AP%22%3C%2FSPAN%3E%20%26amp%3B%20lastRow)%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20filterRange.AutoFilter%20field%3A%3D%3CSPAN%20class%3D%22%22%3E1%3C%2FSPAN%3E%2C%20Criteria1%3A%3D%3CSPAN%20class%3D%22%22%3E%22CC%22%3C%2FSPAN%3E%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20copyRange.SpecialCells(xlCellTypeVisible).Copy%20tgt.Range(%3CSPAN%20class%3D%22%22%3E%22A1%22%3C%2FSPAN%3E)%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3EEnd%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22%22%3ESub%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3000201%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3001139%22%20slang%3D%22en-US%22%3ERe%3A%20Filter%20Range%20Copy%20Paste%20the%20Value%20and%20Create%20new%20Sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3001139%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F743969%22%20target%3D%22_blank%22%3E%40LearningExcelVBA%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20SplitData()%0A%20%20%20%20Dim%20src%20As%20Worksheet%0A%20%20%20%20Dim%20trg%20As%20Worksheet%0A%20%20%20%20Dim%20lastRow%20As%20Long%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20col%20As%20New%20Collection%0A%20%20%20%20Dim%20itm%20As%20Variant%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20src%3DWorksheets(%22Sheet1%22)%0A%20%20%20%20src.AutoFilterMode%20%3D%20False%0A%20%20%20%20lastRow%20%3D%20src.Range(%22A%22%20%26amp%3B%20src.Rows.Count).End(xlUp).Row%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20For%20r%20%3D%202%20To%20lastRow%0A%20%20%20%20%20%20%20%20itm%20%3D%20CStr(src.Range(%22A%22%20%26amp%3B%20r).Value)%0A%20%20%20%20%20%20%20%20col.Add%20Item%3A%3Ditm%2C%20Key%3A%3Ditm%0A%20%20%20%20Next%20r%0A%20%20%20%20On%20Error%20GoTo%200%0A%20%20%20%20For%20Each%20itm%20In%20col%0A%20%20%20%20%20%20%20%20Set%20trg%20%3D%20Nothing%0A%20%20%20%20%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20%20%20%20%20Set%20trg%20%3D%20Worksheets(itm)%0A%20%20%20%20%20%20%20%20On%20Error%20GoTo%200%0A%20%20%20%20%20%20%20%20If%20trg%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20trg%20%3D%20Worksheets.Add(After%3A%3DWorksheets(Worksheets.Count))%0A%20%20%20%20%20%20%20%20%20%20%20%20trg.Name%20%3D%20itm%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20trg.UsedRange.ClearContents%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20src.UsedRange.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3Ditm%0A%20%20%20%20%20%20%20%20src.UsedRange.Copy%20Destination%3A%3Dtrg.Range(%22A1%22)%0A%20%20%20%20Next%20itm%0A%20%20%20%20src.AutoFilterMode%20%3D%20False%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3001166%22%20slang%3D%22en-US%22%3ERe%3A%20Filter%20Range%20Copy%20Paste%20the%20Value%20and%20Create%20new%20Sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3001166%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20been%20receiving%20an%20error%20type%20mismatch%20because%20i%20have%20some%20strings%20with%20that%20signs%20%7C%20and%20there%20are%20400%2B%20unique%20values%20in%20Column%20A%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I have been trying to find an way to create multiple sheets using Specific Column data.

 

If Col"A" has multiple duplicate entries then filter single value create the new sheet using that value name, copy all the data and paste into newly added sheet.

 

I am unable to elaborate this thing in words and sorry for my poor English, i have attached an example workbook.

Where Sheet1 has data using Column A code will create multiple sheets. Your help will be much appreciated.

 

Sub CopyPartOfFilteredRange()
    Dim src As Worksheet
        Dim tgt As Worksheet
        Dim filterRange As Range
        Dim copyRange As Range
        Dim lastRow As Long
    
        Set src=ThisWorkbook.Sheets("Sheet1")
        Set tgt = ThisWorkbook.Sheets("Sheet8")
    
        src.AutoFilterMode = False
    
        lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    
        Set filterRange = src.Range("A1:A" & lastRow)
    
        Set copyRange = src.Range("A1:P" & lastRow)
    
        filterRange.AutoFilter field:=1, Criteria1:="CC"
    
        copyRange.SpecialCells(xlCellTypeVisible).Copy tgt.Range("A1")
    
    End Sub

 

 

6 Replies

@LearningExcelVBA 

Sub SplitData()
    Dim src As Worksheet
    Dim trg As Worksheet
    Dim lastRow As Long
    Dim r As Long
    Dim col As New Collection
    Dim itm As Variant
    Application.ScreenUpdating = False
    Set src=Worksheets("Sheet1")
    src.AutoFilterMode = False
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    On Error Resume Next
    For r = 2 To lastRow
        itm = CStr(src.Range("A" & r).Value)
        col.Add Item:=itm, Key:=itm
    Next r
    On Error GoTo 0
    For Each itm In col
        Set trg = Nothing
        On Error Resume Next
        Set trg = Worksheets(itm)
        On Error GoTo 0
        If trg Is Nothing Then
            Set trg = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            trg.Name = itm
        Else
            trg.UsedRange.ClearContents
        End If
        src.UsedRange.AutoFilter Field:=1, Criteria1:=itm
        src.UsedRange.Copy Destination:=trg.Range("A1")
    Next itm
    src.AutoFilterMode = False
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

@Hans Vogelaar  

 

I have been receiving an error type mismatch because i have some strings with that signs | and there are 400+ unique values in Column A

best response confirmed by LearningExcelVBA (Occasional Contributor)
Solution

@LearningExcelVBA 

You should always post a relevant example instead of one simplified too far.

 

| is allowed in a sheet name, but the following characters aren't: \ , / , * , ? , : , [ , ].

To get rid of those:

Sub SplitData()
    Dim src As Worksheet
    Dim trg As Worksheet
    Dim lastRow As Long
    Dim r As Long
    Dim col As New Collection
    Dim itm As Variant
    Dim c As Variant
    Application.ScreenUpdating = False
    Set src=Worksheets("Sheet1")
    src.AutoFilterMode = False
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    On Error Resume Next
    For r = 2 To lastRow
        itm = CStr(src.Range("A" & r).Value)
        col.Add Item:=itm, Key:=itm
    Next r
    On Error GoTo 0
    For Each itm In col
        For Each c In Array("\", "/", "*", "?", ":", "[", "]")
            itm = Replace(itm, c, "_")
        Next c
        Set trg = Nothing
        On Error Resume Next
        Set trg = Worksheets(itm)
        On Error GoTo 0
        If trg Is Nothing Then
            Set trg = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            trg.Name = itm
        Else
            trg.UsedRange.ClearContents
        End If
        src.UsedRange.AutoFilter Field:=1, Criteria1:=itm
        src.UsedRange.Copy Destination:=trg.Range("A1")
    Next itm
    src.AutoFilterMode = False
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
I apologize for the mistakes but now there is new problem that is name is exceeding more characters than 31. How to keep the limit to 31 character if it is long.

@LearningExcelVBA 

The following will truncate the names to 31 characters. You will have a problem if there are names that differ only beyond the 31st character. For example thisisaridiculouslylongnamenumberone and thisisaridiculouslylongnamenumbertwo. Both will be truncated to thisisaridiculouslylongnamenumb.

Sub SplitData()
    Dim src As Worksheet
    Dim trg As Worksheet
    Dim lastRow As Long
    Dim r As Long
    Dim col As New Collection
    Dim itm As Variant
    Dim c As Variant
    Application.ScreenUpdating = False
    Set src=Worksheets("Sheet1")
    src.AutoFilterMode = False
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    On Error Resume Next
    For r = 2 To lastRow
        itm = CStr(src.Range("A" & r).Value)
        col.Add Item:=itm, Key:=itm
    Next r
    On Error GoTo 0
    For Each itm In col
        For Each c In Array("\", "/", "*", "?", ":", "[", "]")
            itm = Replace(itm, c, "_")
        Next c
        itm = Left(itm, 31)
        Set trg = Nothing
        On Error Resume Next
        Set trg = Worksheets(itm)
        On Error GoTo 0
        If trg Is Nothing Then
            Set trg = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            trg.Name = itm
        Else
            trg.UsedRange.ClearContents
        End If
        src.UsedRange.AutoFilter Field:=1, Criteria1:=itm
        src.UsedRange.Copy Destination:=trg.Range("A1")
    Next itm
    src.AutoFilterMode = False
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
Sir thank you very much