VBA Excel advance filter data and copy to worksheets

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
75
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I have a data set that contains a years worth of data, I would like to filter the data by month and paste this to its own sheet, my data is columns A-N and my months are listed in column M.

I have a range in column R which also lists the months Jan-Dec.

However I get an error on this line - Sheets(sht).Range("M1:M" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("R1"), Unique:=True

"Run-time error '1004': The extract range has a missing or illegal field name."

Does anyone know how to fix?

VBA Code:
Sub filter()

Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String

sht = "Data"

last = Sheets(sht).Cells(Rows.Count, "M").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:N" & last)

Sheets(sht).Range("M1:M" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("R1"), Unique:=True

For Each x In Range([R2], Cells(Rows.Count, "R").End(xlUp))
With rng
.AutoFilter
.AutoFilter Field:=13, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy

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

Sheets(sht).AutoFilterMode = False

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

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Ok, how about
VBA Code:
Sub TaskMaster()
   Dim Cl As Range
   Dim Dic As Object
   Dim i As Long
   
   Application.ScreenUpdating = False
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Data")
      For Each Cl In Range("M2", .Range("M" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Empty
      Next Cl
      For i = 0 To Dic.Count - 1
         .Range("A1:N1").AutoFilter 2, "Difference"
         .Range("A1:N1").AutoFilter 13, Dic.Keys()(i)
         Sheets.Add(, Sheets(Sheets.Count)).Name = Dic.Keys()(i)
         .AutoFilter.Range.Copy Sheets(Dic.Keys()(i)).Range("A1")
      Next i
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Hi Fluff - Just tested and works perfectly. Thank you for all your help!
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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