VBA Filter Spreadsheet by values, then create new tabs

ChloeSpurge

New Member
Joined
May 5, 2021
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hi, The bellow code is meant to filter to each value in column J, then copy the data into a new excel tab. However it gets stuck on
Sheets(sht).Range("J1:J" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AN1"), Unique:=True

Any ideas how I could fix it? FYI my data's last column is AM.

1662029946363.png


Sub filter()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String

'specify sheet name in which the data is stored
sht = "CR Form 2022"

'change filter column in the following code
last = Sheets(sht).Cells(Rows.Count, "J").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:AM" & last)

Sheets(sht).Range("J1:J" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AN1"), Unique:=True

For Each x In Range([AN2], Cells(Rows.Count, "AN").End(xlUp))

With rng
.AutoFilter
.AutoFilter Field:=10, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy

Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.Paste
End With
Next x

' Turn off filter
Sheets(sht).AutoFilterMode = False

With Application
.CutCopyMode = False
.ScreenUpdating = True
End With

End Sub





Many thanks :)
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What do you have in J1 (the column J heading) ?
It is indicating that you have "special" characters in that cell.
 
Upvote 0
OK my guess is that the column name changed and you have something different to that in AN1.
If that is the case add the clear content line as shown below.
If your data that used to run to AM now runs to AN or further right, then we need to address where we are putting the output of the unique values.

Note: The 2nd line if just to show where to put the clear contents
Rich (BB code):
    Range("AN1", Cells(1, "AN").End(xlDown)).ClearContents 
    
    Sheets(sht).Range("J1:J" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AN1"), Unique:=True
 
Upvote 0

Forum statistics

Threads
1,224,836
Messages
6,181,251
Members
453,027
Latest member
Lost_in_spreadsheets

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top