Results 1 to 13 of 13

Macro for advance filter to create separate worksheet and workbook based on criteria

Threaded View

  1. #1
    Registered User
    Join Date
    07-25-2014
    Location
    India
    MS-Off Ver
    2007 , 2010
    Posts
    9

    Macro for advance filter to create separate worksheet and workbook based on criteria

    Hi , I'm trying to create worksheet based on the criteria . It is creating the worksheet based on the criteria but it is not copying the data to respective worksheet
    . I want S.no also to arranaged correctly in all the worksheet . I have attached the sample workbook along with it .


    I also want to know whether it is possible to create workbook using this code by changing worksheet to workbook . Sorry if my question is stupd . i'm not good in Vba . I'm learning it

    Option Explicit
     Sub ExtractToSheets()
        Dim ws     As Worksheet
        Dim wsNew  As Worksheet
        Dim rData  As Range
        Dim rCl    As Range
        Dim sNm    As String
        Set ws = Sheet1
         
         'extract a list of unique names
         'first clear existing list
        With ws
            Set rData = .Range(.Cells(1, 1), .Cells(.Rows.Count, 4).End(xlUp))
            .Columns(.Columns.Count).Clear
            .Range(.Cells(4, 4), .Cells(.Rows.Count, 4).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Cells(4, .Columns.Count), Unique:=True
             
            For Each rCl In .Range(.Cells(1, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp))
                sNm = rCl.Text
                 'add new sheet
                If WksExists(sNm) Then
                     'so clear contents
                    Sheets(sNm).Cells.Clear
                Else
                     'new sheet required
                    Set wsNew = Sheets.Add
                    wsNew.Move After:=Worksheets(Worksheets.Count) 'move to end
                    wsNew.name = sNm
                End If
                 'AutoFilter & copy to relevant sheet
                rData.AutoFilter Field:=2, Criteria1:=sNm
                rData.Copy Destination:=Worksheets(sNm).Cells(1, 1)
            Next rCl
        End With
        ws.Columns(Columns.Count).ClearContents 'remove temporary list
        rData.AutoFilter 'switch off AutoFilter
    End Sub
     
    Function WksExists(wksName As String) As Boolean
        On Error Resume Next
        WksExists = CBool(Len(Worksheets(wksName).name) > 0)
    End Function
    Attached Files Attached Files
    Last edited by bossrockzz; 08-17-2014 at 04:07 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Error In Macro Code
    By greenfalcon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2013, 10:00 PM
  2. [SOLVED] Error in code run a macro once a day
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-26-2013, 10:44 PM
  3. Autofilter Macro Failing - Run-time error 91 - full macro code is attached
    By evamarie in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2013, 02:36 PM
  4. Macro code error: code is near complete
    By kashshaikh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-17-2011, 03:22 PM
  5. Macro code error
    By marshall23 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-05-2011, 03:30 PM

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