VBA Macro to Filter Excel Data and Create Separate Sheets Based on Unique Values

Introduction

Prerequisites

  • A basic understanding of Excel.
  • Familiarity with the VBA editor in Excel.
  • A sample Excel workbook with data

VBA Macro to Filter Excel Data

Step-by-Step Guide – VBA Macro to Filter Excel Data

Press Alt + F11 on your keyboard. This will open the VBA editor.

Step 2: Insert a New Module

  1. Go to the Insert menu.
  2. Click on Module. This will create a new module where you can write your code.

Step 3: Copy and Paste the VBA Code

Sub AutoFilter_With_Create_NewSheets()
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim Last_R As Long
    Dim sht As String
    ‘specify sheet name in which the data is stored
    sht = “Data Set”
    ‘change filter column in the following code
    Last_R = Sheets(sht).Cells(Rows.Count, “A”).End(xlUp).Row
    Set rng = Sheets(sht).Range(“A1:H” & Last_R)
    Sheets(sht).Range(“A1:A” & Last_R).AdvancedFilter Action:=xlFilterCopy, copytorange:=Range(“AA1”), Unique:=True
    For Each x In Range((“AA2”), Cells(Rows.Count, “AA”).End(xlUp))
        With rng
            .AutoFilter
            .AutoFilter field:=1, Criteria1:=x.Value
            .SpecialCells(xlCellTypeVisible).Copy
            Sheets.Add(after:=Sheets(Sheets.Count)).Name = x.Value
            ActiveSheet.Paste
            Range(“A1”).CurrentRegion.Columns.AutoFit
        End With
    Next x
    ‘ turns off filter
    Sheets(sht).AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 
VBA Macro to Filter Excel Data

Step 4: Understand the Code

Step 5: Run the Macro

  1. Close the VBA editor.
  2. Press Alt + F8 to open the Macro dialog box.
  3. Select AutoFilter_With_Create_NewSheets.
  4. Click Run.
VBA Macro to Filter Excel Data

Step 6: Verify the Results

Conclusion

Related Post

Leave a Comment

Your email address will not be published. Required fields are marked *