Forum Discussion
Bala Subramanian
Jul 03, 2017Copper Contributor
Macro in excel sheet 2010
Hello Friends, I need to create a Macro in MS excel 2010 so that I can divide a file into several files based on the filtering of a particular column. ie, One column of the excel file will conta...
Matt Mickle
Aug 15, 2017Bronze Contributor
Here is sample code to filter values on Sheet1 by a person's name. In this example the names are Matt, John and Jared.... If you paste this code into the attached workbook and change the SaveAs file path you should be able to get close to what you want:
Sub CreateMultipleWBs()
Dim arrFilter As Variant
arrFilter = Array("Matt", "John", "Jared") 'Define Array
'Arrays start at 0 so if we have 3 elements then we need to go from 0 - 2
For intLp = 0 To 2
With Sheets("Sheet1")
lCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'Define last column
lrow = .Cells(Rows.Count, "A").End(xlUp).Row 'Define last row
.AutoFilterMode = False 'UnFilter Data from previous...
With .Range(Cells(1, 1), Cells(lrow, lCol))
.AutoFilter 1, "=" & arrFilter(intLp) 'Filter X Name (i.e. Matt, John, Jared) 1 = Column A or Column #1
.SpecialCells(xlCellTypeVisible).Copy 'Delete Everything but the header... offset resizes range
End With
Workbooks.Add 'Add Workbook
Range("A1").PasteSpecial 'Paste Data
ActiveWorkbook.SaveAs Filename:="C:\Users\mmickle1\Desktop\" & arrFilter(intLp) & ".xlsx" 'Save File
ActiveWorkbook.Close 'Close new workbook
End With
Next intLp 'Go to next value to filter in our array
End Sub