VBA Autofilter Check for "A123" (letter, number number number)?

svendiamond

Well-known Member
Joined
Jun 13, 2014
Messages
1,504
Office Version
  1. 365
Platform
  1. Windows
Hey guys, how can I use Range.Autofilter to filter out a general alphanumeric code such as "A123" or "X029" (letter followed by three numbers)? I've been Googling and trying everything and I can't get it. The following will show me anything that begins with a letter... but that's as far as I've got:

Code:
Range("A1:J1").AutoFilter Field:=7, Criteria1:=">;"

Basically I have a list of terminal codes and sometimes they are incorrect. They should always be "A123" or "X987" so, if there is anything that isn't formatted like that, ("i.e. 'A12' or 'XYZA'") then I want autofilter to show me all the incorrect ones.

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Maybe you can use advanced filter with a formula

Assuming the codes in column A, beginning in row 2, to show only the incorrect values the formula would be something like this
=OR(LEN(A2)<>4,ISNUMBER(0+LEFT(A2)),ISERROR(0+RIGHT(A2,3)))

Hope this helps

M.
 
Upvote 0
Thanks, Marcelo. Was about to post that I used a workaround formula.

Mine was

=IF(OR(LEN(G2)<>4,ISNUMBER(VALUE(LEFT(G2,1))),NOT(ISNUMBER(VALUE(RIGHT(G2,3))))),"ERROR","OK")

But yours works too, and is a bit cleaner.

Thank you!
 
Upvote 0
Great you found a solution!

You are welcome. Thanks for the feedback.

M.
 
Upvote 0

Forum statistics

Threads
1,216,609
Messages
6,131,723
Members
449,667
Latest member
PSAv

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