VBA Excel advance filter data and copy to worksheets

TaskMaster

New Member
Joined
Oct 15, 2020
Messages
20
Office Version
  1. 365
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
 

TaskMaster

New Member
Joined
Oct 15, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi Yes, cells in column B will either display "Match" or "Difference"
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
67,943
Office Version
  1. 365
Platform
  1. Windows
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
 

TaskMaster

New Member
Joined
Oct 15, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi Fluff - Just tested and works perfectly. Thank you for all your help!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
67,943
Office Version
  1. 365
Platform
  1. Windows
My pleasure.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,783
Messages
5,766,437
Members
425,354
Latest member
kshivanand21

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
Top