I have some VBA code that filters a set of data, creates a worksheet titled after each filter, and then copies/pastes the data into the correct worksheet.
Last week it was only pasting the values, but this week it started pasting the formulas. I only want to paste the values.
However, when I go to modify the code to pastespecial, it causes excel to completely crash and close. If I just have .Paste it works fine. What am I doing wrong?
Sub filter()
Application>ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String
On Error Resume Next
sht = "Sheet1"
last = Sheets(sht).Cells(Rows.Count, "D").End(x1Up).Row
Set rng = Sheets(sht).Range("A1:T" & last)
Sheets(sht).Range("D2:D" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("CA1"), Unique:=True
For Each x In Range ([CA2], Cells(Rows.Count, "CA").End(xlUp))
With rng
.AutoFilter
.AutoFilter Field:=4, Criterial:=x.Value
.SpecialCells(xlCellTypeVisible).Copy
Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.PasteSpecial Paste:=xlPasteValues
End With
Next x
Sheets(sht).AutoFilterMode = False
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
Last week it was only pasting the values, but this week it started pasting the formulas. I only want to paste the values.
However, when I go to modify the code to pastespecial, it causes excel to completely crash and close. If I just have .Paste it works fine. What am I doing wrong?
Sub filter()
Application>ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String
On Error Resume Next
sht = "Sheet1"
last = Sheets(sht).Cells(Rows.Count, "D").End(x1Up).Row
Set rng = Sheets(sht).Range("A1:T" & last)
Sheets(sht).Range("D2:D" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("CA1"), Unique:=True
For Each x In Range ([CA2], Cells(Rows.Count, "CA").End(xlUp))
With rng
.AutoFilter
.AutoFilter Field:=4, Criterial:=x.Value
.SpecialCells(xlCellTypeVisible).Copy
Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.PasteSpecial Paste:=xlPasteValues
End With
Next x
Sheets(sht).AutoFilterMode = False
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With