Excel VBA CodeTutorials

Excel Advanced Filters to split data into separate sheets using VBA

3 Mins read

Excel Advanced filters technique are useful when you deal with a large dataset. In this blog, we will try to cover one of Excel Advanced filter techniques to understand how to split data into a separate sheet through VBA code (it’s a part of our Excel VBA tutorial program).

For instance, we have a huge list of U.S. records and we need to split the data as per state. Now imagine there are almost 51 states in U.S. and you need to apply an Excel Advanced filters and then choose one by one and then copy and paste the same into a new sheet. It will take almost one hour or more as it is very tedious work. Just use the macro code on this page and it will take not more than one minute to complete the task. That’s why VBA code in excel is so powerful.

This is an advanced excel features where you are going to learn how to filter and split data into seperate sheets using VBA. But, if I tell you that we can visualize data in to Excel Dashboard with the help of Data Visualization in Excel, If you don’t know What is Excel Dashboard then you can navigate to Courses > Excel Dashboard Course > Enroll Now and you will learn Excel Dashboard

Let’s have a look at the example:

Split-data-using-Excel-Advanced-Filter

You have this type of data sets like above picture. However, If you check very carefully then you will see that the 6th column from the left contains the name of the state and you need to separate the records into a new sheet and paste the records on the new sheet.

Recommended:

Any idea about what’s the best part of Building dashboards in Excel? It can be utilized to simplify your data into more controllable amounts of visual information. This permits you to see what you are doing right and where is the necessity of improvement. When utilized properly, Excel dashboard tutorials can be used to help you make well-versed decisions. Hence, it impacts the performance of your business. This will, in turn, impact your bottom line. Interested to Learn about Excel Dashboards? Learn with us

How to Apply Excel Advanced filters using a VBA code

Copy the entire filtered dataset and paste it into a new sheet. Along with this, change the name of the worksheet based on the filtered tab. For example, if it copies the records of LA (State) and pastes it in a new sheet then it will change the name of a sheet as LA.

You can also split data using Text to column

VBA Coding Part

Copy the below VBA code and paste into a module.

To do the same please press Alt + F11 and then insert a module and paste the below code.

Sub ExtractToSheets()
Dim ws As Worksheet
Dim wsNew As Worksheet
Dim rData As Range
Dim rfl As Range
Dim state As String
Set ws = ThisWorkbook.Sheets("&")
With ws
Set rData = .Range(.Cells(1, 1), .Cells(.Rows.Count, 11).End(xlUp))
.Columns(.Columns.Count).Clear
.Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Cells(1, .Columns.Count), Unique:=True
'Calls Excel Advanced Filter
For Each rfl In .Range(.Cells(1, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp))
state = rfl.Text
If WksExists(state) Then
Sheets(state).Cells.Clear
Else
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = state
End If
rData.AutoFilter Field:=6, Criteria1:=state
rData.Copy Destination:=Worksheets(state).Cells(1, 1)
Next rfl
End With
ws.Columns(Columns.Count).ClearContents
rData.AutoFilter
End Sub
Function WksExists(wksName As String) As Boolean
On Error Resume Next
WksExists = CBool(Len(Worksheets(wksName).Name) & 0)
End Function
'Excel advanced filter technique

I have done is created an Excel Advanced filter using a VBA code. All you need to edit the code to select the filtered parameter while calling the Excel Advanced filter and call this macro when you need.

Now go back to your excel sheet and create a shape and assign the code to that shape. Right-click on that shape and then click on assign. Then below box will appear and select the macro name and then click OK.

Assign-Macro-help-Excel-Advanced-Filter

Next Step

Click on the button you will see that individual sheet is created. Also, the records associated with that state are placed on the new sheet. Sheet name is changed as per records.

Separate-Sheet-VBA-Excel-Advanced-Filter

First of all, I have applied an Excel advanced filter and stored. Then in a variable and then checked whether the state name already exists or not. If the state name exists in the book then it will clear the data on the sheet. And if the sheet does not present in the workbook then it will create a new sheet and paste the data on that sheet. Please practice the same and play with the macro code. separate

Related posts
AlteryxTutorials

Alteryx Hotkeys - Alteryx Keyboard Shortcuts

2 Mins read
Top 50+ Alteryx Shortcuts for Windows. Alteryx is popularly known as a Self-Service Analytics tool. Business users can build their data workflows…
AlteryxTutorials

Step By Step Guide to Learn Alteryx

6 Mins read
Alteryx Learning Path: The growth in technology has resulted in growth in understanding. In today’s world, humans – fellow businessmen know the…
Excel VBA CodeTutorials

VBA Code to Clean the Date Format

1 Mins read
When it is useful? Most of the time the most annoying problem is when the data is taken from ERP or other…

9 Comments

Comments are closed.