Re: Using CriteriaRange to exclude blank cells in a Range with AdvancedFilter XLS2003 SOLVED
…..
The criteria you are using only define the beginning of the strings, …
…If you want a criterion for 7 characters (and only 7) in which the 4th is "-", 2 ways:
1 - with the cell formatted as General use:
="=???-???"
2 - First format the cell as text and then use:
=???-???
….
. Thanks for that contribution PGC. (and the gem of info that despite hours of googling I could not find!! )
……
. That’s going some way to clearing things up and returning a small piece of my sanity – (Excel was probably somewhere along the lines of my experimenting “guessing” formats and throwing a Spanner in the works!) – your 2 suggested solutions Appear to work for me.
. I am still wondering if there is any known logic to these solutions or it is all based on guess work with experienced users like you “guessing” it mostly right!!
……………
.. you asked for it!!!!
…… going back to something similar to my original examples from post #6, but for clarity and consistence with Andrew Poulsom using E column for criteria range.. and trying any of your 2 solutions gives ( Filtered column A results tacked on at end of main unfiltered table as before ): ( ….and in the screen shot I chose option Values so we are “seeing” here the text format from the formula which I typed in as formula
="=???-???"
……
……..
1 - with the cell formatted as General use:
="=???-???"
2 - First format the cell as text and then use:
=???-???
(which is the same value in the criterion cell as the one in option 1)
………..
…..and which Excel is evaluating out as the Text =???-??? (… that is making some sense to me now..…) )
…values screen shot after running code:-
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]A
[/td][td]B
[/td][td]C
[/td][td]D
[/td][td]E
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][td]Name[/td][td]Km[/td][td][/td][td]NumberPlate[/td][/tr]
[tr][td]2
[/td][td]MIN-423[/td][td]name 1[/td][td]0,0 [/td][td][/td][td]=???-???[/td][/tr]
[tr][td]3
[/td][td]MDN-229[/td][td]name 2[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]4
[/td][td]MMG-561[/td][td]name 4[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]5
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]6
[/td][td]MDN-229[/td][td]name 5[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]7
[/td][td]MDN-22953[/td][td]name 6[/td][td]0,9 [/td][td][/td][td][/td][/tr]
[tr][td]8
[/td][td]MDN-229[/td][td]name 7[/td][td]0,6 [/td][td][/td][td][/td][/tr]
[tr][td]9
[/td][td]hdshg[/td][td]name 8[/td][td]0,7 [/td][td][/td][td][/td][/tr]
[tr][td]10
[/td][td]MDN-229[/td][td]name 9[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]11
[/td][td]MAE-745[/td][td]name 10[/td][td]1,0 [/td][td][/td][td][/td][/tr]
[tr][td]12
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]13
[/td][td]NumberPlate[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]14
[/td][td]MIN-423[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]15
[/td][td]MDN-229[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]16
[/td][td]MMG-561[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]17
[/td][td]MAE-745[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
….. code again for clarity..
Code:
[color=darkgreen]'[/color]
[color=blue]Sub[/color] PoulsonPJC5()
[color=blue]Dim[/color] lr [color=blue]As[/color] [color=blue]Long[/color], rng [color=blue]As[/color] Range, c [color=blue]As[/color] Range, lc [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Dim[/color] wks1 [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wks1 = ThisWorkbook.Worksheets("unfiltered") [color=darkgreen]'set sheet name - Give abbreviation for "unfiltered" sheet in ThisWorkbook all Objects, Properties and Methods of Object Worksheet obtainable to view in the intellisense given after typing . Dot[/color]
wks1.Range("A13:A200").ClearContents
[color=darkgreen]'Part 1) Optional Start Bit to Delete Sheets / Tabs------------[/color]
Application.DisplayAlerts = [color=blue]False[/color] [color=darkgreen]'Prevents being asked everytime if you really want to delete the Workbook[/color]
[color=blue]Dim[/color] ws [color=blue]As[/color] Worksheet [color=darkgreen]'Tempory worksheet name. ws now has Methods and Properties of Worksheets obtained with typing . dot after it![/color]
[color=blue]For[/color] [color=blue]Each[/color] ws [color=blue]In[/color] ActiveWorkbook.Worksheets [color=darkgreen]'We could alternatively use something like ThisWorkbook, Workbooks("Itsname.xlxm")[/color]
[color=blue]If[/color] ws.Name <> "ASheetToKeep" And ws.Name <> wks1.Name [color=blue]Then[/color] [color=darkgreen]'Check that Worksheet name is not that of any that you want (Name property here returns name without .xlsm bit on end)[/color]
ws.Delete
[color=blue]Else[/color] [color=darkgreen]'Presumably then the worksheet name is That of the first sheet or any you wish to keep[/color]
[color=darkgreen]' do nothing (Don't delete it!)[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] ws
Application.DisplayAlerts = [color=blue]True[/color] [color=darkgreen]'Turn it back on[/color]
[color=darkgreen]'---End Bit to delete any Sheets / Tabs--------------------[/color]
[color=darkgreen]'Part 2) Produce New sheets based on valid Number Plates[/color]
lr = wks1.Cells(Rows.Count, "A").End(xlUp).Row
lc = 3
wks1.Range("A1:A" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wks1.Range("E1:E2"), CopyToRange:=wks1.Range("A" & lr + 2), Unique:=[color=blue]True[/color]
[color=blue]Set[/color] rng = wks1.Range("A" & lr + 3, wks1.Cells(Rows.Count, 1).End(xlUp))
[color=blue]For[/color] [color=blue]Each[/color] c [color=blue]In[/color] rng [color=darkgreen]' Each c is each range, that is to say each cell in the entire range rng[/color]
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
wks1.Range("A1", wks1.Cells(lr, lc)).AutoFilter 1, c.Value
wks1.Range("A1", wks1.Cells(lr, lc)).SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1")
wks1.AutoFilterMode = [color=blue]False[/color]
[color=blue]Next[/color]
[color=darkgreen]'wks1.Range("A" & lr + 2, wks1.Cells(Rows.Count, 1).End(xlUp)).ClearContents 'empty extra takt on Unique values in LookUpColumn[/color]
[color=darkgreen]'End part to produce new sheets--------------------------[/color]
wks1.Activate [color=darkgreen]'Activate sheet 1 just to see it[/color]
[color=blue]End[/color] [color=blue]Sub[/color]
.. similarly with Andrews last suggestion for exclusion criteria ( noting it will only work if I remove the empty cell in Column A ):
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]A
[/td][td]B
[/td][td]C
[/td][td]D
[/td][td]E
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][td]Name[/td][td]Km[/td][td][/td][td]NumberPlate[/td][/tr]
[tr][td]2
[/td][td]MIN-423[/td][td]name 1[/td][td]0,0 [/td][td][/td][td]<>MMG-561[/td][/tr]
[tr][td]3
[/td][td]MDN-229[/td][td]name 2[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]4
[/td][td]MMG-561[/td][td]name 4[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]5
[/td][td]Not Empty[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]6
[/td][td]MDN-229[/td][td]name 5[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]7
[/td][td]MDN-22953[/td][td]name 6[/td][td]0,9 [/td][td][/td][td][/td][/tr]
[tr][td]8
[/td][td]MDN-229[/td][td]name 7[/td][td]0,6 [/td][td][/td][td][/td][/tr]
[tr][td]9
[/td][td]hdshg[/td][td]name 8[/td][td]0,7 [/td][td][/td][td][/td][/tr]
[tr][td]10
[/td][td]MDN-229[/td][td]name 9[/td][td]0,0 [/td][td][/td][td][/td][/tr]
[tr][td]11
[/td][td]MAE-745[/td][td]name 10[/td][td]1,0 [/td][td][/td][td][/td][/tr]
[tr][td]12
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]13
[/td][td]NumberPlate[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]14
[/td][td]MIN-423[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]15
[/td][td]MDN-229[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]16
[/td][td]Not Empty[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]17
[/td][td]MDN-22953[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]18
[/td][td]hdshg[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]19
[/td][td]MAE-745[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
…… I am still experimenting with endless combinations of criteria ranges to achieve a combination of your “accepted” format criteria and Andrews exclusion criteria to get for example this:
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]A
[/td][td]B
[/td][td]C
[/td][td]D
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][td]Name[/td][td]Km[/td][td][/td][/tr]
[tr][td]2
[/td][td]MIN-423[/td][td]name 1[/td][td]0,0 [/td][td][/td][/tr]
[tr][td]3
[/td][td]MDN-229[/td][td]name 2[/td][td]0,0 [/td][td][/td][/tr]
[tr][td]4
[/td][td]MMG-561[/td][td]name 4[/td][td]0,0 [/td][td][/td][/tr]
[tr][td]5
[/td][td]Not Empty[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]6
[/td][td]MDN-229[/td][td]name 5[/td][td]0,0 [/td][td][/td][/tr]
[tr][td]7
[/td][td]MDN-22953[/td][td]name 6[/td][td]0,9 [/td][td][/td][/tr]
[tr][td]8
[/td][td]MDN-229[/td][td]name 7[/td][td]0,6 [/td][td][/td][/tr]
[tr][td]9
[/td][td]hdshg[/td][td]name 8[/td][td]0,7 [/td][td][/td][/tr]
[tr][td]10
[/td][td]MDN-229[/td][td]name 9[/td][td]0,0 [/td][td][/td][/tr]
[tr][td]11
[/td][td]MAE-745[/td][td]name 10[/td][td]1,0 [/td][td][/td][/tr]
[tr][td]12
[/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]13
[/td][td]NumberPlate[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]14
[/td][td]MIN-423[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]15
[/td][td]MDN-229[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]16
[/td][td]MAE-745[/td][td][/td][td][/td][td][/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
… simply increasing the criteria range to CriteriaRange:=wks1.Range("E1:E3") with this
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]E
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][/tr]
[tr][td]2
[/td][td]<>MMG-561[/td][/tr]
[tr][td]3
[/td][td]=???-???[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
Or
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]E
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][/tr]
[tr][td]2
[/td][td]=???-???[/td][/tr]
[tr][td]3
[/td][td]<>MMG-561[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
does not appear to work. - It appears to give results as if CriteriaRange:=wks1.Range("E1:E2"), so ignoring the second criteria!!?!
………………..
. This is strange as my multiple criteria ( all “inclusion” criteria*** ) from code 1 ( CriteriaRange:=wks1.Range("C20:C24") ) appears to work for multiple criteria..
.. ( first as with my original format ="MDN-229" etc )
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]A
[/td][td]B
[/td][td]C
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][td]Name[/td][td]Km[/td][/tr]
[tr][td]2
[/td][td]MIN-423[/td][td]name 1[/td][td]0,0 [/td][/tr]
[tr][td]3
[/td][td]MDN-229[/td][td]name 2[/td][td]0,0 [/td][/tr]
[tr][td]4
[/td][td]MMG-561[/td][td]name 4[/td][td]0,0 [/td][/tr]
[tr][td]5
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]6
[/td][td]MDN-229[/td][td]name 5[/td][td]0,0 [/td][/tr]
[tr][td]7
[/td][td]MDN-22953[/td][td]name 6[/td][td]0,9 [/td][/tr]
[tr][td]8
[/td][td]MDN-229[/td][td]name 7[/td][td]0,6 [/td][/tr]
[tr][td]9
[/td][td]hdshg[/td][td]name 8[/td][td]0,7 [/td][/tr]
[tr][td]10
[/td][td]MDN-229[/td][td]name 9[/td][td]0,0 [/td][/tr]
[tr][td]11
[/td][td]MAE-745[/td][td]name 10[/td][td]1,0 [/td][/tr]
[tr][td]12
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]13
[/td][td]NumberPlate[/td][td][/td][td][/td][/tr]
[tr][td]14
[/td][td]MIN-423[/td][td][/td][td][/td][/tr]
[tr][td]15
[/td][td]MDN-229[/td][td][/td][td][/td][/tr]
[tr][td]16
[/td][td]MMG-561[/td][td][/td][td][/td][/tr]
[tr][td]17
[/td][td]MDN-22953[/td][td][/td][td][/td][/tr]
[tr][td]18
[/td][td]MAE-745[/td][td][/td][td][/td][/tr]
[tr][td]19
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]20
[/td][td][/td][td][/td][td]NumberPlate[/td][/tr]
[tr][td]21
[/td][td][/td][td][/td][td]MIN-423[/td][/tr]
[tr][td]22
[/td][td][/td][td][/td][td]MDN-229[/td][/tr]
[tr][td]23
[/td][td][/td][td][/td][td]MMG-561[/td][/tr]
[tr][td]24
[/td][td][/td][td][/td][td]MAE-745[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
… or additionally with the improved format from PGC ( ="=MDN-229" etc )
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]A
[/td][td]B
[/td][td]C
[/td][/tr]
[tr][td]1
[/td][td]NumberPlate[/td][td]Name[/td][td]Km[/td][/tr]
[tr][td]2
[/td][td]MIN-423[/td][td]name 1[/td][td]0,0 [/td][/tr]
[tr][td]3
[/td][td]MDN-229[/td][td]name 2[/td][td]0,0 [/td][/tr]
[tr][td]4
[/td][td]MMG-561[/td][td]name 4[/td][td]0,0 [/td][/tr]
[tr][td]5
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]6
[/td][td]MDN-229[/td][td]name 5[/td][td]0,0 [/td][/tr]
[tr][td]7
[/td][td]MDN-22953[/td][td]name 6[/td][td]0,9 [/td][/tr]
[tr][td]8
[/td][td]MDN-229[/td][td]name 7[/td][td]0,6 [/td][/tr]
[tr][td]9
[/td][td]hdshg[/td][td]name 8[/td][td]0,7 [/td][/tr]
[tr][td]10
[/td][td]MDN-229[/td][td]name 9[/td][td]0,0 [/td][/tr]
[tr][td]11
[/td][td]MAE-745[/td][td]name 10[/td][td]1,0 [/td][/tr]
[tr][td]12
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]13
[/td][td]NumberPlate[/td][td][/td][td][/td][/tr]
[tr][td]14
[/td][td]MIN-423[/td][td][/td][td][/td][/tr]
[tr][td]15
[/td][td]MDN-229[/td][td][/td][td][/td][/tr]
[tr][td]16
[/td][td]MMG-561[/td][td][/td][td][/td][/tr]
[tr][td]17
[/td][td]MAE-745[/td][td][/td][td][/td][/tr]
[tr][td]18
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]19
[/td][td][/td][td][/td][td][/td][/tr]
[tr][td]20
[/td][td][/td][td][/td][td]NumberPlate[/td][/tr]
[tr][td]21
[/td][td][/td][td][/td][td]=MIN-423[/td][/tr]
[tr][td]22
[/td][td][/td][td][/td][td]=MDN-229[/td][/tr]
[tr][td]23
[/td][td][/td][td][/td][td]=MMG-561[/td][/tr]
[tr][td]24
[/td][td][/td][td][/td][td]=MAE-745[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
unfiltered[/td][/tr][/table]
…
… the madness continues… maybe “inclusion” ( “accepted” )*** and “exclusion” criteria are not allowed together???
Alan
P.s. just a minor extra bit of madness noticed along the way….. a strange syntax anomaly:…
…….. ….this syntax is not allowed
Code:
wks1.Range("A1:A" & lr).AdvancedFilter Action:=xlFilterCopy, , CopyToRange:=wks1.Range("A" & lr + 2), Unique:=True
… but thess are….
Code:
wks1.Range("A1:A" & lr).AdvancedFilter xlFilterCopy, , CopyToRange:=wks1.Range("A" & lr + 2), Unique:=True
wks1.Range("A1:A" & lr).AdvancedFilter xlFilterCopy, , wks1.Range("A" & lr + 2), True