
Click on the link below to check out the working macroĭownload Workbook Related Link - Filter Data Based on Drop Down Selection For example I would enter value 1 in cell AA2 as I want to select 1 in column F ('rank' column). Next step is to enter the value you want to filter in cell AA2 and make sure nothing is filled in the whole column AA except cell AA2.
#EXCEL VBA INSERT NEW SHEET AND RENAME IT HOW TO#
How to filter only one value instead of all the unique values It assumes your data starts from column A. It also writes 'Total' at last row of column A. The following program sums column B of each worksheet. Suppose you want to sum values in each worksheet.
#EXCEL VBA INSERT NEW SHEET AND RENAME IT CODE#
You can remove this line of code if you don't want to close the newly created workbooks. Data for each unique value will be saved in a different workbook with the name same as unique value Incase you want to paste data to multiple workbooks after filtering data. Workbk.Sheets(sht).AutoFilterMode = FalseĨ. (After:=Sheets(Sheets.Count)).Name = x.Value Workbk.Sheets(sht).Range("F1:F" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=Trueįor Each x In Workbk.Sheets(sht).Range(, Cells(Rows.Count, "AA").End(xlUp)) Last = Workbk.Sheets(sht).Cells(Rows.Count, "F").End(xlUp).Row Set newBook = Workbooks.Add(xlWBATWorksheet) Suppose you want to filter and paste data to a new workbook instead of adding sheets in the same workbook. 'Specify sheet name in which the data is storedħ. To change columns which need to be copied, make changes in this line of code. In the program below, it copies columns A, D and F Suppose you want to copy specific columns instead of all the columns in your sheet. If Not GetWorksheet(x.Text) Is Nothing ThenĦ. Caution : It removes the existing worksheets which were created via macro. (ii) Add the following lines of code after 'For Each x In Range(, Cells(Rows.Count, "AA").End(xlUp))'. (i) Add the following function before sub filter().įunction GetWorksheet(shtName As String) As Worksheet To workaround this issue, follow the steps below. If you run the macro more than once, the error would occur as sheets already exist. AutoFilter Field:= 6, Criteria1:=x.Valueĥ. In this case, 6 refers to column index number (i.e. Change the value in this part of the code. Unique values of column F are stored in column AA.Ĥ. Change filter column (column F) and starting cell of range (A1) in the code.ģ. Change the below line of code in the program.Ģ. Specify name of the sheet in which data is stored. How to Filter and Paste Values to New Workbookġ. Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value Sheets(sht).Range("F1:F" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=Trueįor Each x In Range(, Cells(Rows.Count, "AA").End(xlUp)) Set rng = Sheets(sht).Range("A1:F" & last) Last = Sheets(sht).Cells(Rows.Count, "F").End(xlUp).Row 'change filter column in the following code 'specify sheet name in which the data is stored In this case, it creates four worksheets - 1, 2, 3, 4 as these are unique values in column Rank (column F). This macro would filter a column and paste distinct values to the sheets with their respective names. In the following excel macro, it is assumed a filter is applied on column F (Rank) and data starts from cell A1.Įxcel Macro : Filter and Paste Unique Values to New Sheets

Save the file as Macro Enabled Workbook (xlsm) or Excel 97-2003 Workbook (xls).It can be easily done with Excel VBA programming. You have to do it 50 times which is a tedious and error-prone task.

For example, you have a column in which there are 50 unique values. It is a very time consuming process if you do it manually. In other words, this needs to be done for each unique values in a column in which we have applied filter. Suppose you are asked to apply filter on a column and paste result of a filter into a new worksheet or workbook and same process goes until all the unique values of the column are covered.
