TaskMaster
Board Regular
- Joined
- Oct 15, 2020
- Messages
- 75
- Office Version
- 365
- 2016
- Platform
- 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?
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