I recorded a macro that utilizes the "Advanced Filter" feature in Excel.
I have a criteria that has 5 possible values. They are: na, e, e/p, p, s.
The macro filters each of those 5 values, individually, then I have some formulas that count the number of entries for each of those 5 possible values. The problem I have is that if I filter the "e" value and there are no rows that contain an "e", I get the count for "e/p", like it's defaulting to the closest match.
Is there a way to prevent this from happening, the count for "e", if there are none, should be 0 (zero).
Here's a portion of the code:
I realize that writing a macro that loops through all this stuff would be much better, but I can't write code worth a darn, and usually record something and then modify when necessary.
I have a criteria that has 5 possible values. They are: na, e, e/p, p, s.
The macro filters each of those 5 values, individually, then I have some formulas that count the number of entries for each of those 5 possible values. The problem I have is that if I filter the "e" value and there are no rows that contain an "e", I get the count for "e/p", like it's defaulting to the closest match.
Is there a way to prevent this from happening, the count for "e", if there are none, should be 0 (zero).
Here's a portion of the code:
Code:
Application.ScreenUpdating = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"'[COLOR="Red"]this is where the value gets changed[/COLOR]
Range("J2").Select
ActiveCell.FormulaR1C1 = "0"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O1").Select'[COLOR="red"]this is where the count is returned[/COLOR]
Selection.Copy
Range("u1").Select'[COLOR="red"]this is where the count is pasted to for each of the 5 possible values and this is where I get the wrong values I mentioned[/COLOR]
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "1"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O2").Select
Selection.Copy
Range("u2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "2"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O3").Select
Selection.Copy
Range("u3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "3"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O4").Select
Selection.Copy
Range("u4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "4"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O5").Select
Selection.Copy
Range("u5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "5"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O6").Select
Selection.Copy
Range("u6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "6"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O7").Select
Selection.Copy
Range("u7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "7"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O8").Select
Selection.Copy
Range("u8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e"
Range("J2").Select
ActiveCell.FormulaR1C1 = "8"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O9").Select
Selection.Copy
Range("u9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub FilterRunStyleSheet_test()
Application.ScreenUpdating = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "0"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O1").Select
Selection.Copy
Range("x1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "1"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O2").Select
Selection.Copy
Range("x2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "2"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O3").Select
Selection.Copy
Range("x3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "3"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O4").Select
Selection.Copy
Range("x4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "4"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O5").Select
Selection.Copy
Range("x5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "5"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O6").Select
Selection.Copy
Range("x6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "6"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O7").Select
Selection.Copy
Range("x7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "7"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O8").Select
Selection.Copy
Range("x8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("I2").Select
ActiveCell.FormulaR1C1 = "e/p"
Range("J2").Select
ActiveCell.FormulaR1C1 = "8"
Range("I11").Select
Range("A11:N94").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"G1:J2"), CopyToRange:=Range("HA1:HN1"), Unique:=False
Range("O9").Select
Selection.Copy
Range("x9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
I realize that writing a macro that loops through all this stuff would be much better, but I can't write code worth a darn, and usually record something and then modify when necessary.