using a wildcard to prevent subtotal line from being filtered and copied excel vba

Gabe Carvajal

I have a situation where I have a list of salespeople that gets filtered and moves all that filtered data to it's own spreadsheet. The problem that I am having is the macro is also filtering the subtotal line so its creating a sheet with no data and it's also creating a situation where the sheet is so large I cant save the file.

I wrote some code that i thought would prevent any worksheet starting with "Sheet" to not get filtered, but I don't know how to use a wildcard in a string. Need a wildcard since the "Sheet #" is different depending on the month.

Dim Sht As Worksheet
        Dim Rng As Range
        Dim List As Collection
        Dim varValue As Variant
        Dim E As Long

    '   // Set your Sheet name
        Set Sht = Application.ActiveSheet

    '   // set your auto-filter,  A6
        With Sht.Range("A2")
            .AutoFilter
        End With

    '   // Set your agent Column range # (2) that you want to filter it
        Set Rng = Range(Sht.AutoFilter.Range.Columns(22).Address)


ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add 
Key:=Range _
         ("V:V"), SortOn:=xlSortOnValues, Order:=xlAscending, 
DataOption:= _
            xlSortTextAsNumbers
        With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

    '   // Create a new Collection Object
        Set List = New Collection

    '   // Fill Collection with Unique Values
        On Error Resume Next
        For E = 2 To Rng.Rows.Count
            List.Add Rng.Cells(E, 1), CStr(Rng.Cells(E, 1))
        Next E

    '   // Start looping in through the collection Values
        For Each varValue In List


    '       // Filter the Autofilter to macth the current Value
            'Rng.AutoFilter Field:=22, Criteria1:=varValue, _
             '   Operator:=xlAnd, Criteria2:="<>"

            Rng.AutoFilter Field:=22, Criteria1:="<>Sheet*", _
                Operator:=xlAnd, Criteria2:=varValue

    '       // Copy the AutoFiltered Range to new Workbook
            'If List = (Blanks) Then
            Sht.AutoFilter.Range.Copy

The Criteria1:="<>Sheet*" code is what I tried to do and the code above is what is was before. So my question is what can be done to prevent to the subtotal row sheet from being created?

Example of how the first tab

Gabe Carvajal

So after playing with the code for awhile I realized that using a wildcard for "Sheet*" wouldn't work since all the filtered results started with "Sheet" in the first place. But by adding

If varValue <> "" Then (which states that if the filtered result isn't blank than continue code) after Rng.AutoFilter Field:=22, Criteria1:="<>Sheet*", Operator:=xlAnd, Criteria2:=varValue

it fixed the issue. Now the code skips the subtotal line successfully.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prevent line numbers from being copied to clipboard

From Dev

Prevent session from being replicated when JSESSIONID cookie copied

From Dev

Can I prevent object from being copied by std::memcpy?

From Dev

Is there a very effective method to prevent a .jpg file for being copied from a page?

From Dev

Excel to Javascript - Ignore new line characters in a column copied from Excel

From Dev

How to exclude some files from being copied using xcopy?

From Dev

Excel VBA wildcard search

From Dev

Prevent war file from being copied to .m2/repository when running mvn clean install

From Dev

How to prevent part of HTML text from being copied when copying adjacent?

From Dev

Prevent C# WPF BitmapSource bytes from being copied before render

From Dev

Prevent QR code from being copied and QR code should be scanable by my mobile app only

From Dev

How to prevent part of HTML text from being copied when copying adjacent?

From Dev

protect images from being copied

From Dev

Finding Average and Standard Deviation on displayed, filtered data in Excel Using VBA

From Dev

Select and copy a specific number of filtered rows using VBA in excel

From Dev

VBA to return nth row number from a filtered table in excel

From Dev

Array with data from filtered and ordered listobject in excel 2007 vba

From Dev

Excel VBA - Check if file exists using Wildcard and open the file

From Dev

VBA Prevent Charts from AutoUpdating in Excel

From Dev

Excel VBA prevent from importing missing references

From Dev

How do I prevent F1 from opening help in Excel 2013, without using VBA?

From Dev

Excel - using wildcard in IF formula

From Dev

Excel - IF function using a wildcard

From Dev

vba excel - find string wildcard

From Dev

Prevent Excel from clearing copied data for pasting, after certain operations, without Office clipboard

From Dev

Sum only visibile cells using SUBTOTAL in Excel

From Dev

How to prevent binary file folder also being copied to output dir?

From Dev

Avoid the li tag symbol from being copied

From Dev

Protecting raw JSON data from being copied

Related Related

  1. 1

    Prevent line numbers from being copied to clipboard

  2. 2

    Prevent session from being replicated when JSESSIONID cookie copied

  3. 3

    Can I prevent object from being copied by std::memcpy?

  4. 4

    Is there a very effective method to prevent a .jpg file for being copied from a page?

  5. 5

    Excel to Javascript - Ignore new line characters in a column copied from Excel

  6. 6

    How to exclude some files from being copied using xcopy?

  7. 7

    Excel VBA wildcard search

  8. 8

    Prevent war file from being copied to .m2/repository when running mvn clean install

  9. 9

    How to prevent part of HTML text from being copied when copying adjacent?

  10. 10

    Prevent C# WPF BitmapSource bytes from being copied before render

  11. 11

    Prevent QR code from being copied and QR code should be scanable by my mobile app only

  12. 12

    How to prevent part of HTML text from being copied when copying adjacent?

  13. 13

    protect images from being copied

  14. 14

    Finding Average and Standard Deviation on displayed, filtered data in Excel Using VBA

  15. 15

    Select and copy a specific number of filtered rows using VBA in excel

  16. 16

    VBA to return nth row number from a filtered table in excel

  17. 17

    Array with data from filtered and ordered listobject in excel 2007 vba

  18. 18

    Excel VBA - Check if file exists using Wildcard and open the file

  19. 19

    VBA Prevent Charts from AutoUpdating in Excel

  20. 20

    Excel VBA prevent from importing missing references

  21. 21

    How do I prevent F1 from opening help in Excel 2013, without using VBA?

  22. 22

    Excel - using wildcard in IF formula

  23. 23

    Excel - IF function using a wildcard

  24. 24

    vba excel - find string wildcard

  25. 25

    Prevent Excel from clearing copied data for pasting, after certain operations, without Office clipboard

  26. 26

    Sum only visibile cells using SUBTOTAL in Excel

  27. 27

    How to prevent binary file folder also being copied to output dir?

  28. 28

    Avoid the li tag symbol from being copied

  29. 29

    Protecting raw JSON data from being copied

HotTag

Archive