VBA autofilter

i_malc

New Member
Joined
Aug 28, 2010
Messages
14
Can anyone help me please? I’m new to VBA and I’ve written code to run an AutoFilter. It’s only a few lines and I should be able to see what the error is but just cant. It runs and it shows the headings and deletes the rest of the range.

Sub StartEnd()

Dim StDate As Long: StDate = (AO1)
Dim EndDate As Long: EndDate = (AP1)

Set sh = ThisWorkbook.Sheets("sheet1")
sh.AutoFilterMode = False
Range("A1:AN4900").AutoFilter 11, ">=" & StDate ', xlAnd, "<=" & EndDate
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this:
Dim StDate As Long: StDate =
Range("AO1")
Dim EndDate As Long: EndDate = Range("AP1")
 
Last edited:
Upvote 0
Thanks, But its still doesn’t filter. The column with the date in is named “Batched” and the date is in format “1 Feb 06” does the column need formatting?
KeyCodeTtypeAcctRefTdateGoodsVatPxRefPxDateBalBatchPeriodCurrentYourRefGratesVratesPmethodsNCodes
16INVDOD012473611999111016.72.9219.6222 Nov 992000011016.72.92250
17INVDOD01247479199911105.50.966.4622 Nov 99200001105.50.96250
18INVDOD01247744199911129.21.6110.8122 Nov 99200001109.21.61250
19INVDOD012479251999111516.52.8919.3922 Nov 992000011016.52.89250
20INVDOD0124812119991116123.9621.69145.6522 Nov 9920000110123.9621.69250

<colgroup><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"><col style="text-align: center;"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
In order for it to work, the column with the dates and the cell holding the Start and End dates must ALL be entered as valid dates. If eny are entered as Text, it will not work properly.
What are your date values in cells "AO1" and "AP1"?
 
Last edited:
Upvote 0
But are those entries and the entries in your "BATCH" column ALL entered as dates, or are some entered as text?

Note that based on the sample data you posted, NONE of those entries meet your filter criteria.
 
Upvote 0
Thanks for your help so far but I've tried different formats in cells AO1 & AP1. I think it might be the Batch column. Is there any way to convert the column to date format? If possible to email you my file.
 
Upvote 0
Changing the cell format will not make a difference - it is the data type that must be changed.

Try the following and tell me what happens:
- Temporarily change the cell formats cells AO1, AP1, and your BATCH column to "GENERAL". What happens? Do they all change to big numbers, like 40000?
If they do NOT, then that is the cells that we must fix, as the entries are text entries and not date entries.
 
Upvote 0
Both AO1 & AP1 gov to high numbers but Batch dosen't. Can anything be done to change Batch to date format, If not I've notice that column 5 Tdate has a date in reverse order can anything be done with this column?
 
Upvote 0
You should be able to use Text to Columns to fix both of those.

For the Batch column do the following:
1. Select the entire column
2. Choose "Text to Columns" from the "Data"
3. On Step 1, choose the "Delimited" option and click Next
4. Hit Next again to get to Step 3
5. On Step 3, choose the "Date" radio button, and then "DMY date format option from the drop-down box
6. Click Finish

That should fix that column.

For your other column, follow the same steps, except choosing the "YMD" date format option.
Note those indicate the order of the data ("YMD" means Year is first, then Month, then Date). Then it knows how to read and convert it.

Once those values are converted to valid dates using that technique, your Filters should work, and you can also choose any date format that you want for those columns.
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,325
Members
449,154
Latest member
pollardxlsm

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