+ Reply to Thread
Results 1 to 12 of 12

VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290

    VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Hello,
    Good day everyone. Hope you guys are doing great.

    I am looking for a vba based solution for the subject mentioned tasks. (almost novice in vba)

    I found this code (sheet attached) somewhere on the web, it works find but I need some modification and I hope you guys will help.


    Modifications required:

    1) Instead of this hard coded range, we need an input box to enter column name (eg: "A")

    Set rngFilter = Range("G1", Range("G" & Rows.Count).End(xlUp))
    
    Set rngUniques = Range("G1", Range("G" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
    2) Before saving we need in each file, Convert the data into Excel Table and add a Total row and sum formula in column E (Table total row).


    3) Sort Column E (Largest to smallest)

    Any or all help is appreciated.

    Regards,
    Attached Files Attached Files
    Khalid

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Change first part to

    Sub Filter_Split()
    Dim wbDest As Workbook, rngFilter As Range, rngUniques As Range, cell As Range
        
    mycol = InputBox("Witch Column ?")
    Set rngFilter = Range(mycol & "1", Range(mycol & Rows.Count).End(xlUp))
    Application.ScreenUpdating = False
    With rngFilter
        .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
        Set rngUniques = Range(mycol & "1", Range(mycol & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
        ' Clear the filter
        ActiveSheet.ShowAllData
    End With
    Kind regards
    Leo

  3. #3
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Compleet

    Sub Filter_Split()
    Dim wbDest As Workbook, rngFilter As Range, rngUniques As Range, cell As Range
        
    mycol = InputBox("Witch Column ?")
    Set rngFilter = Range(mycol & "1", Range(mycol & Rows.Count).End(xlUp))
    Application.ScreenUpdating = False
    With rngFilter
        .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
        Set rngUniques = Range(mycol & "1", Range(mycol & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
        rngUniques.Copy Sheets("blad1").Range("C1")
        ' Clear the filter
        ActiveSheet.ShowAllData
    End With
    For Each cell In rngUniques
        Set wbDest = Workbooks.Add(xlWBATWorksheet)
        rngFilter.AutoFilter Field:=1, Criteria1:=cell.Value
        rngFilter.EntireRow.Copy
        With wbDest.Sheets(1).Range("A1")
                .PasteSpecial xlPasteColumnWidths           'Paste column widths
                .PasteSpecial xlPasteValuesAndNumberFormats 'Paste values
                .PasteSpecial Paste:=xlPasteFormats         'Paste formats ######
        End With
        Application.CutCopyMode = True
            
            'completely added by me on 8 nov 2015
            
            'For copy Title
    '        ThisWorkbook.Sheets(1).Range("A1:H5").Copy
            
            'For Paste Title
    '        With wbDest.Sheets(1).Range("A1:H5")
     '           .PasteSpecial xlPasteColumnWidths           'Paste column widths
      '          .PasteSpecial xlPasteValuesAndNumberFormats 'Paste values
       '         .PasteSpecial Paste:=xlPasteFormats         'Paste formats ######
        '    End With
            
            'For Copy End Lines
         '   ThisWorkbook.Worksheets("Sheet1").Range("A19:H22").Copy
            
            'For Paste End Lines at the end of the Sheet
          '  wbDest.Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1).Select
           ' ActiveSheet.Paste
                
            
            
            'For page Setup
            ActiveSheet.PageSetup.PrintArea = myRange
            With ActiveSheet.PageSetup
                .PrintTitleRows = ""
                .PrintTitleColumns = ""
                .LeftMargin = Application.InchesToPoints(0.2)
                .RightMargin = Application.InchesToPoints(0.2)
                .TopMargin = Application.InchesToPoints(0.5)
                .BottomMargin = Application.InchesToPoints(0.5)
                .HeaderMargin = Application.InchesToPoints(0.2)
                .FooterMargin = Application.InchesToPoints(0.2)
                .PaperSize = xlPaperA4
                .Orientation = xlLandscape 'xlPortrait
                .Zoom = False
                .FitToPagesWide = 1
                .FitToPagesTall = 1
    '            .PrintTitleRows = "$1:$6"
                '.PrintArea = "$A$1:$K$57"
                .LeftFooter = "&""Cambria,Regular""&11Fahad Insaf-Distribution Department"
                .CenterFooter = "&""Cambria,Regular""&11PharmEvo (Pvt.) LTD."
     '           .RightFooter = "&""Cambria,Regular""&11Page &P of &N"
                .CenterHorizontally = True
                .CenterVertically = False
            End With
            
            
            
            ' {END} completely added by me on 8 nov 2015
            
            ' Name the destination sheet
            wbDest.Sheets(1).Name = cell.Value
            ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "Tabel1"
            ActiveWorkbook.ActiveSheet.ListObjects("Tabel1").Sort.SortFields.Add _
            Key:=Range("Tabel1[B. Assigned to MS]"), SortOn:=xlSortOnValues, Order:= _
            xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.ListObjects("Tabel1").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("E" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=SUM(Tabel1[B. Assigned to MS])"
        Range("D" & Rows.Count).End(xlUp).Offset(2) = "Total"
    
            
            'Save the destination workbook and close
            
           
            'Added By Fahad
    '        wbDest.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path _
     '       & Application.PathSeparator & cell.Value, _
      '      Quality:=xlQualityStandard, IncludeDocProperties:=True, _
       '     IgnorePrintAreas:=False, OpenAfterPublish:=False
            
            wbDest.SaveAs ThisWorkbook.Path & Application.PathSeparator & _
                cell.Value
            wbDest.Close False 'Close the new workbook
        Next cell
        rngFilter.Parent.AutoFilterMode = False
        Application.ScreenUpdating = True
    End Sub
    Kind regards
    Leo

  4. #4
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Hi Leo,
    Thanks for the help.

    I am having error:
    Screen shots attached.

    at the following:
    rngUniques.Copy Sheets("blad1").Range("C1")
    Regards,
    Attached Images Attached Images

  5. #5
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    I forgot to remove that line, you dont need it


    Kind regards
    Leo

  6. #6
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Hi Leo,
    It works like a magic

    I will check this on complete file on Monday, and will give my feedback.

    Really appreciate your help.
    Blessing to you,

    Khalid

  7. #7
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290
    Hi Leo,
    Good day.

    My colleagues are much happy with your solution.
    Thakns for your help.

    Just a follow up question:
    Is it possible to add one more inputbox for the sorting?
    I mean we can enter the column name in inputbox and it will sort all the files based on inputbox?

    Appreciate if you could help on this too.

    With regards,
    Khalid

  8. #8
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    So 1 inputbox for Column to create the Files
    and 1 inputbox to say on witch Column to sort

    and in inputbox the columnheaders or A,B,C ..... ?

    Kind regards
    Leo

  9. #9
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290
    Hi Leo

    Yes exactly, you have already helped for the fist inputbox, which is working great.
    Need 2nd inputbox for sorting on columns before saving each file. 2nd inputbox can be any type of, e.g: A,B,C or table header name e.g: name, b.assigned etc. Whichever is possible.

    I think A,B,C will be much easier for user.

    Many thanks,
    Khalid
    Last edited by Khalidngo; 03-13-2017 at 11:06 PM.

  10. #10
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Try this and let me know

    Sub Filter_Split()
    Dim wbDest As Workbook, mystring As String, mycol As String, mycolnr1 As Integer, mycolnr2 As Integer, ws As Worksheet
    Set ws = Sheets("Sheet1")
    ws.Range("A1").CurrentRegion.Copy
    Application.ScreenUpdating = False
    With Sheets.Add
        .Name = "Temp"
        .PasteSpecial xlPasteColumnWidths
        With ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
            .LeftMargin = Application.InchesToPoints(0.2)
            .RightMargin = Application.InchesToPoints(0.2)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .HeaderMargin = Application.InchesToPoints(0.2)
            .FooterMargin = Application.InchesToPoints(0.2)
            .PaperSize = xlPaperA4
            .Orientation = xlLandscape 'xlPortrait
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            '.PrintTitleRows = "$1:$6"
            '.PrintArea = "$A$1:$K$57"
            .LeftFooter = "&""Cambria,Regular""&11Fahad Insaf-Distribution Department"
            .CenterFooter = "&""Cambria,Regular""&11PharmEvo (Pvt.) LTD."
            '.RightFooter = "&""Cambria,Regular""&11Page &P of &N"
            .CenterHorizontally = True
            .CenterVertically = False
        End With
    End With
    arr = ws.Range("A1").CurrentRegion
    ReDim sn(1 To UBound(arr), 1 To UBound(arr, 2))
    mycol = InputBox("Witch Column for Creation of files ?")
    mycol2 = InputBox("Witch Column for Sorting ?")
    mycolnr2 = ws.Range(mycol2 & "1").Column
    mycolnr1 = ws.Range(mycol & "1").Column
    For x = 2 To UBound(arr)
        If InStr(1, mystring, arr(x, mycolnr1)) = 0 Then
            ws.Range("A1").CurrentRegion.AutoFilter mycolnr1, Criteria1:=arr(x, mycolnr1)
            mystring = mystring & " " & arr(x, mycolnr1)
            ws.Range("A1").CurrentRegion.SpecialCells(12).Copy Sheets("Temp").Range("A1")
            Sheets("Temp").Range("A1").CurrentRegion.Sort Key1:=Range(mycol2 & "1"), Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, _
                MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
            Sheets("Temp").Copy
            ActiveSheet.Name = arr(x, mycolnr1)
            ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "Tabel1"
            Range("E" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=SUM(Tabel1[B. Assigned to MS])"
            Range("D" & Rows.Count).End(xlUp).Offset(2) = "Total"
            ActiveWorkbook.SaveAs ThisWorkbook.Path & Application.PathSeparator & arr(x, mycolnr1)
            ActiveWorkbook.Close
            ws.Range("A1").CurrentRegion.AutoFilter
            Sheets("Temp").Range("A1").CurrentRegion.Clear
        End If
    Next
    Application.DisplayAlerts = False
    Sheets("Temp").Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub
    Kind regards
    Leo

  11. #11
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Hi Leo,

    That works great on the sample. I am sure it will work the same way on my actual file, I will test tomorrow and will let you know.

    Many thanks and have a good day.

    Regards,

    Khalid

  12. #12
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: VBA Macro to Filter with Input box, Split files, Convert into Excel Table and Sort

    Hi Leo,

    I have tried on my actual file and it works like a dream

    Please accept my sincere gratitude for your help.

    God bless you.

    Regards,

    Khalid

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA/macro to split an excel file into multiple files with 500 lines each
    By karambos3 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-11-2023, 12:43 PM
  2. Need to convert .ods files into .xlsx using excel macro
    By ranjita_bls in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-24-2015, 06:15 AM
  3. Convert multiple word files to one excel table
    By Barefaced in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2014, 04:52 AM
  4. Excel Macro to convert list of files to pdf
    By nandu05 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-05-2013, 12:52 AM
  5. How to convert XML files into an EXCEL table (rows)?
    By pstein in forum Excel General
    Replies: 0
    Last Post: 01-10-2013, 03:48 AM
  6. Excel 2010 Macro -how to split worksheet into multiple files
    By kbustin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-01-2012, 10:39 AM
  7. User Input to Split the Table
    By rakesh1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2010, 08:11 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