+ Reply to Thread
Results 1 to 11 of 11

VBA Macro required for filtered data to a new sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2013
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    10

    VBA Macro required for filtered data to a new sheet.

    Could anyone help me to get macro for the following:
    I have an excel data in a worksheet. I want this data to be filtered for column A header. Say for example "name" is the header for column A. Now this "name" should be filtered and the each of the filtered rows with their header are required to be copied in a new worksheet. The filtered cell value shall be given for the name for the new sheets.

    Example: Here the Name A shall be filtered and copied in a new sheet"A", Name B to sheet"B", Name C to sheet"C".......to indefinite numbers.

    Name Hd2 Hd3 Hd4 Hd5
    A X1 X1 X1 X1
    A Y1 Y1 Y1 Y1
    A Z1 Z1 Z1 Z1
    B X1 X1 X1 X1
    B Y1 Y1 Y1 Y1
    B Z1 Z1 Z1 Z1
    C X1 X1 X1 X1
    C Y1 Y1 Y1 Y1
    C Z1 Z1 Z1 Z1
    D X1 X1 X1 X1
    D Y1 Y1 Y1 Y1
    D Z1 Z1 Z1 Z1


    Thank you friends,
    Last edited by anbub1; 04-03-2013 at 10:27 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Macro required for filtered data to a new sheet.

    Hi anbub1

    This link does just as you describe...if you need help adapting please post your File.

    http://www.excelforum.com/excel-prog...ual-files.html
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    02-06-2013
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Macro required for filtered data to a new sheet.

    Thank you so much.

    Your link matched my requirement. I did little alteration in the VBA code. I removed the codes sorting with colors. Finally I could not find the saved new workbooks anywhere in my system in XP OS. But I could see these files were stored in downloads location in vista OS. So I deleted the close option and made the files open, so that I can close these files manually.

    Is it possible to make file storage location as D:/<folder name>? How to fix it?

    Regards,
    Anbub

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Macro required for filtered data to a new sheet.

    Hi Anbub

    Post your Code and one of us will look at it. Please remember to use Code Tags when you do so.

  5. #5
    Registered User
    Join Date
    02-06-2013
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Macro required for filtered data to a new sheet.

    Dear Mr. John,

    Thank you for your reply. With this attached vba code, I can manage to get separate excel files for every change in first column of sheet "Original". I want these files to be saved with passkeys available in the sheet "Dummy". Passkeys for the first column of sheet "Dummy" is corresponding entry of column 2. These files are to be stored in my D drive & folder Products.

    I hope, I have explained you clearly.

    Thank you once again.

    Regards,

    Anbub
    Attached Files Attached Files

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Macro required for filtered data to a new sheet.

    Hi Anbub

    Try this Code. Please note, a Dynamic Named Range titled "ItemCode" has been added to Sheet Dummy... let me know of issues
    Option Explicit
    Sub CC_List()
        Dim wb As Workbook
        Dim newBook As Workbook
        Dim ws As Worksheet
        Dim ws1 As Worksheet
        Dim LR As Long
        Dim Rng As Range
        Dim cel As Range
        Dim myPath As String
        Dim SavePath As String
        Dim pw As String
    
        myPath = ThisWorkbook.Path & "\"
        SavePath = "D:\Products\"
        '    SavePath = "E:\Products\" '<---jaslake Path for testing...can be deleted
    
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Original")
        Set ws1 = Sheets("Dummy")
    
        Application.ScreenUpdating = False
        If Not Evaluate("ISREF(Lists!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Lists"
        Else
            Sheets("Lists").Cells.Clear
        End If
    
        With ws
            LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=Range("A2:A" & LR), _
                    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange Range("A2:I" & LR)
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
    
            .Columns(1).Copy Sheets("Lists").Range("A1")
            Sheets("Lists").Columns("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
            ActiveWorkbook.Names.Add Name:="Depts", RefersTo:= _
                    "=OFFSET(Lists!$A$2,0,0,(COUNTA(Lists!$A:$A)-1),1)"
    
            For Each cel In Range("Depts")
                .Range("A1:I" & LR).AutoFilter Field:=1, Criteria1:=cel
    
                Set newBook = Workbooks.Add(xlWBATWorksheet)
    
                With newBook
                    With ws.Range("A1:I" & LR)
                        .Copy
                        newBook.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
                        newBook.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteAll
                        newBook.Sheets(1).Name = cel
                    End With
    
                    With ws1.Range("ItemCode")
                        Set Rng = .Find(What:=cel, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                        If Not Rng Is Nothing Then
                            pw = Rng.Offset(0, 1).Value
                        Else
                            MsgBox "Password not found for " & cel & vbCrLf & "Password has been set to """
                            pw = ""
                        End If
    
                    End With
    
                    Application.DisplayAlerts = False
                    .SaveAs SavePath & cel, FileFormat:=51, Password:=pw
                    newBook.Close
                    Application.DisplayAlerts = True
                End With
                .AutoFilterMode = False
            Next cel
            .AutoFilterMode = False
            Application.DisplayAlerts = False
            Sheets("Lists").Delete
            Application.DisplayAlerts = True
        End With
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    Last edited by jaslake; 04-01-2013 at 06:10 PM.

  7. #7
    Registered User
    Join Date
    02-06-2013
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Macro required for filtered data to a new sheet.

    Dear Mr. John,

    Thanks for your reply. But still I am held up at line "With ws1.Range("ItemCode")". I am getting Run time error '1004': Method 'Range' of object '_Worksheet' failed.

    Could you please help me?

    Regards,

    Anbub

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Macro required for filtered data to a new sheet.

    Hi Anbub

    As I pointed out here, you need to Create this Range
    Please note, a Dynamic Named Range titled "ItemCode" has been added to Sheet Dummy
    If you don't know how to do that let me know and I'll have the Code do it.

  9. #9
    Registered User
    Join Date
    02-06-2013
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Macro required for filtered data to a new sheet.

    Thank you so much Mr. John,

    Its working perfectly. Thank you once again.

    Regards,

    Anbub

  10. #10
    Registered User
    Join Date
    02-06-2013
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    10

    Thumbs up Re: VBA Macro required for filtered data to a new sheet.

    Thank you so much Mr. John,

    Its working perfectly. Thank you once again.

    Regards,

    Anbub

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Macro required for filtered data to a new sheet.

    You're welcome...glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1