VBA Excel advance filter data and copy to worksheets

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
55
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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Approximately how many rows of data do you have?
Also is Col M text values or actual dates formatted to show the month?
 
Upvote 0
Hi there are over 4000 rows, being split into the 12 months of the year. Column M is formatted as January, February....
 
Upvote 0
So col M contains dates & not text?
 
Upvote 0
Hi, col M is formatted with the full month name.
 

Attachments

  • Capture.PNG
    Capture.PNG
    23.5 KB · Views: 19
Upvote 0
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 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
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
Hi, just had a thought. Would it be possible to add a second variable. For example can we only copy cells in column B that say the word difference to the monthly tabs?
 
Upvote 0
Is the word "difference" on it's own in the cell?
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,267
Members
448,558
Latest member
aivin

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