Excel Create Worksheet per Filter Value | VBA Macro #16

Share on facebook
Share on twitter
Share on linkedin
This is an Excel macro that will filter a column and create a new worksheet for each unique value found. By default, the active column selected will be filtered. If the active column selected is blank an InputBox will ask for the column number to filter. (Example: A=1, B=2, C=3, etc.) Once a column has been identified the duplicate values will be removed and what remains will be used to determine how many worksheets to create. The Excel Create Worksheet per Filter Value macro will then pull in the relevant information into each worksheet. This macro is an efficient way to view your data in separate worksheets. You could filter and checkmark one option at a time but this is labor some, especially if you want to analyze each value independently. It takes even more time to copy and paste information into its own worksheet. Using VBA, you can eliminate the manual work and complete all these actions at once!

See it in Action!

Watch this video to see this macro in action.

The Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Excel 16.0 Object Library, OLE Automation, Microsoft Office 16.0 Object Library
'Leverage & Lean "Less Clicks, More Results"
Sub CreateWorksheetFilterValue()
' Means variable is in use
Dim ActiveColumn As Integer '
Dim ActiveWorksheetName As String '
Dim Counter As Integer '
Dim DupLastColumnLetter As String '
Dim DupLastRow As Integer '
Dim LastColumnLetter As String '
Dim LastColumnNumber As Integer '
Dim LastRow As Integer '
Dim WorksheetName As String '
Dim WS As Worksheet '

On Error GoTo LeverageLean

ActiveWorksheetName = ActiveSheet.Name 'What is the Active Sheet's Name
LastColumnNumber = Cells(1, Columns.Count).End(xlToLeft).Column 'Identify Last Column Number
LastColumnLetter = Replace(Cells(1, LastColumnNumber).Address(True, False), "$1", "") 'Identify Last Column Letter
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row on worksheet

ActiveColumn = ActiveCell.Column 'What is the Active Column to filter

If ActiveColumn > LastColumnNumber Then
ActiveColumn = InputBox("What column do you want to filter?" & vbNewLine & vbNewLine & "Please use a number A=1, B=2, C=3, etc.") 'Pick a column with data in it
ElseIf ActiveColumn > LastColumnNumber Then
Exit Sub
End If

'Copy Column information and remove duplicates
Columns(ActiveColumn).Select
Selection.Copy
Columns(LastColumnNumber + 2).Select
ActiveSheet.Paste
DupLastColumnLetter = Replace(Cells(1, LastColumnNumber + 2).Address(True, False), "$1", "") 'Identify Column Letter in Duplicate Column
ActiveSheet.Range(DupLastColumnLetter & "1:" & DupLastColumnLetter & LastRow).RemoveDuplicates Columns:=1, Header:=xlYes
DupLastRow = Cells(Rows.Count, LastColumnNumber + 2).End(xlUp).Row 'Identify Last Row Number in Duplicate Column

'If there are more then 50 values Exit the Sub
If DupLastRow > 50 Then
Columns(LastColumnNumber + 2).Delete
Range("A1").Select
MsgBox "Exited because " & DupLastRow & " tabs were going to be created."
Exit Sub
End If

Counter = 2 'Assuming there is a header row

'Loop through each duplicate value copy and paste information to appropriate worksheet
Do Until Counter > DupLastRow
Sheets(ActiveWorksheetName).Select
WorksheetName = Cells(Counter, LastColumnNumber + 2).Value
ActiveSheet.Range("$A$1:$" & LastColumnLetter & "$" & LastRow).AutoFilter Field:=ActiveColumn, Criteria1:=WorksheetName
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Set WS = Sheets.Add(After:=Sheets(Sheets.Count))
WS.Name = WorksheetName 'Use the duplicate value as a tab name
Range("A1").Select
ActiveSheet.Paste
Counter = Counter + 1
Loop

'Clear the filter and delete the duplicates column
Sheets(ActiveWorksheetName).Select
ActiveSheet.AutoFilterMode = False
Range("A1").Select
Columns(LastColumnNumber + 2).Delete

MsgBox ("A Worksheet has been created for each Filter Value!")

Exit Sub

LeverageLean:
MsgBox ("Looks like " & Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")

End Sub
'Stay Awesome

Macro Monday

Here is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!

Customization

These lines of code can be customized to personalize this macro.
40. If DupLastRow > 50 Then – If the filtered column returns more than 50 values the macro will exit to ensure the Excel can handle the request. Update this number to reflect how you want this to behave.
47. Counter = 2 ‘Assuming there is a header row – If you are not working with a header row then you should change Counter = 1
67. ActiveSheet.AutoFilterMode = False – If you don’t want the filter to be removed from the first spreadsheet make this line a comment with ‘
69. Columns(LastColumnNumber + 2).Delete – If you don’t want the duplicates column to be deleted from the first spreadsheet make this line a comment with ‘
71. MsgBox (“A Worksheet has been created for each Filter Value!”) – If you don’t want this macro to finish with a MsgBox then make this line a comment with ‘

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Leverage & Lean

Is focused on improving computer processes by Leveraging programming and applying Lean principles. It is our goal to make you more efficient and effective on your computer producing “Less Clicks and More Results!”