VBA Autofilter with criteria as a cell in a differant worksheet. Getting "runtime error 9"

309 Views Asked by At

I'm work on this project where I have a summary sheet and a data sheet where I want to drop in raw data from a separate report weekly into the data sheet and filter through what I need and copies to the summary sheet.

The start of the code is simply clearing out the cells for a new run each time. The problem I'm having is the one autofilter criteria that is a cell value from the summary sheet. It is a drop down box with that will when changed, copy different data to the summary sheet. Here is my code. Thanks for any advice in advance!

Sub Macro3()
    Sheets("Summary").Select
    Range("C5").Select
    Selection.ClearContents
    Range("C6").Select
    Selection.ClearContents
    Range("C10").Select
    Selection.ClearContents
    Range("C11").Select
    Selection.ClearContents
    Range("C16").Select
    Selection.ClearContents
    Range("C17").Select
    Selection.ClearContents
    Range("C21").Select
    Selection.ClearContents
    Range("C22").Select
    Selection.ClearContents
    Range("F11").Select
    Selection.ClearContents
    Range("F10").Select
    Selection.ClearContents
    Range("F6").Select
    Selection.ClearContents
    Range("F5").Select
    Selection.ClearContents

    Sheets("Data").Select

    Range("K200000").Select
    Selection.ClearContents
    Range("J200000").Select
    Selection.ClearContents

    i = Sheets("Summary").Range("i2")

    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=13, Criteria1:=i
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7, Criteria1:="<>"
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:="<>0"
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:=i
    Range("K200000").Select
    ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,R[-199996]C:R[-1]C)"
    Range("J200000").Select
    ActiveCell.FormulaR1C1 = "=SUBTOTAL(2,R[-199996]C:R[-1]C)"
    Selection.NumberFormat = "#,##0.0"
    Selection.NumberFormat = "#,##0"
    Selection.Copy
    Sheets("Summary").Select
    Range("C5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    Range("k200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:="<>" & i
    Range("j200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    Range("k200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C11").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=12, Criteria1:= _
        "<>"
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:=i
    Range("j200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C16").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    Range("k200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C21").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:="<>" & i
    Range("j200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C17").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    Range("k200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("C22").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=12
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=4, Criteria1:="<>0"
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:=i
    Range("j200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("F5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    Range("k200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("F10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:="<>" & i

    Range("j200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("F6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    Range("k200000").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Summary").Select
    Range("F11").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=13
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=4
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15
    Sheets("Summary").Select
End Sub
1

There are 1 best solutions below

0
On

As Peh says, this is pretty much unreadable. From your question though, I think your problem refers to setting the criteria to i? If so, try changing

i = Sheets("Summary").Range("i2")

to

i = Sheets("Summary").Range("i2").Value