Autofilter Macro Question

THRASHER69

Board Regular
Joined
Mar 29, 2012
Messages
200
Hello,
I'm needing help figuring out my problem. On the code listed below, I can not get the 2nd line to work correctly. Specifically the "=" part. If I change it to "<" or "<>" everything works fine except it is not pulling the correct information and copying it. The C1 value is a Date

THRASHER

Code:
Sheets("Original").Activate
    CriteriaStg = "=" & Range("C1").Value
    SheetsStg = "Orders Taken Yesterday"
    CriteriaTbl = Split(CriteriaStg, ";")
    ShtTbl = Split(SheetsStg, ";")
     
    Sheets("Original").Activate
     
    For I = 0 To UBound(CriteriaTbl, 1)
        WkSht = ShtTbl(I)
        WkCriteria = CriteriaTbl(I)
        Sheets(WkSht).Cells.ClearContents
        With Range("A1").CurrentRegion
            .AutoFilter field:=2, Criteria1:=WkCriteria
            .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(WkSht).Range("A1")
            .AutoFilter field:=2
        End With
    Next I
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I don't know why you need the equal sign in parehthesis when you are later splitting the value. I don't see anything that uses the Range("C1") value where it would need the "=" included in the string variable value. If you later need the equal sign for your autofilter criteria then you could concatenate it there like:
Criteria:= "=" & WkCriteria.
 
Upvote 0
Iwas wondering about having the "=" also but I tried changing the 2nd line from
Code:
[COLOR=#333333]CriteriaStg = "=" & Range("C1").Value[/COLOR]
to
Code:
[COLOR=#333333]CriteriaStg =  Range("C1").Value[/COLOR]
and that did not work either.
 
Upvote 0
Are you getting an error message? If so, what?
 
Upvote 0
If C1 is a date and you use
CriteriaTbl = Split(CriteriaStg, ";")
Then how is it splitting on the ";" separator? Or do you mean C1 is a set of dates?
I don't see anything wrong with the basic code other than the "=". So, it probably is values being returned for the variables that is causing the problem. You can put borders around the cells like A1:D5 and copy it to the thread so we can see what you are working with.
 
Upvote 0
The separator is basically void at the moment. I will be adding to the string once I get this part working like I want. Once finishes, the code will look like below with the first part of the string to copy everything that = the date in C1 to sheet Orders Taken Yesterday and the second part of the string to copy everything less than the date in A1 to sheet Back Orders. The second part of the string works fine. Copy's what it should to the correct sheet. I just can not get the first part to = the date in C1. I can use < or > or <> in place of the + sign and it will work fine. I just can not make the = sign work. I do not get an error message. It just does not copy over for some reason

Code:
Sheets("Original").Activate    
    CriteriaStg = "=" & Range("C1").Value;"<" & Range("A1").Value
    SheetsStg = "Orders Taken Yesterday";"Back Orders"
    CriteriaTbl = Split(CriteriaStg, ";")
    ShtTbl = Split(SheetsStg, ";")
     
    Sheets("Original").Activate
     
    For I = 0 To UBound(CriteriaTbl, 1)
        WkSht = ShtTbl(I)
        WkCriteria = CriteriaTbl(I)
        Sheets(WkSht).Cells.ClearContents
        With Range("A1").CurrentRegion
            .AutoFilter field:=2, Criteria1:=WkCriteria
            .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(WkSht).Range("A1")
            .AutoFilter field:=2
        End With
    Next I
 
Upvote 0
The separator is basically void at the moment. I will be adding to the string once I get this part working like I want. Once finishes, the code will look like below with the first part of the string to copy everything that = the date in C1 to sheet Orders Taken Yesterday and the second part of the string to copy everything less than the date in A1 to sheet Back Orders. The second part of the string works fine. Copy's what it should to the correct sheet. I just can not get the first part to = the date in C1. I can use < or > or <> in place of the + sign and it will work fine. I just can not make the = sign work. I do not get an error message. It just does not copy over for some reason

Code:
Sheets("Original").Activate    
    CriteriaStg = "=" & Range("C1").Value;"<" & Range("A1").Value
    SheetsStg = "Orders Taken Yesterday";"Back Orders"
    CriteriaTbl = Split(CriteriaStg, ";")
    ShtTbl = Split(SheetsStg, ";")
     
    Sheets("Original").Activate
     
    For I = 0 To UBound(CriteriaTbl, 1)
        WkSht = ShtTbl(I)
        WkCriteria = CriteriaTbl(I)
        Sheets(WkSht).Cells.ClearContents
        With Range("A1").CurrentRegion
            .AutoFilter field:=2, Criteria1:=WkCriteria
            .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(WkSht).Range("A1")
            .AutoFilter field:=2
        End With
    Next I

sorry, the + sign should be an = sign. I could not find where to edit my post
 
Upvote 0

Forum statistics

Threads
1,203,324
Messages
6,054,732
Members
444,747
Latest member
Jaborsum

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top