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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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