View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Mark Mark is offline
external usenet poster
 
Posts: 989
Default A little Macro Help Please ...

yeah, the post was unclear because of how this thing breaks lines, but the
whole IF condition, through the the THEN, needed to be on one line, or have a
continuation character put in, as you did.

But I'm not getting the other error that you got in my test.

Here's what I have, for a little sample range of A1:A10

A1: Type, A2 through A10 have 'a' in it, so that the condition will not be
found.

Then, the code is:

Sub test()
Selection.AutoFilter Field:=1, Criteria1:="<32*", Operator:=xlAnd
Range("A2:a10").Select
If Range("a2:a10").SpecialCells(xlCellTypeVisible).Co unt <
Range("a2:a10").Cells.Count Then
Selection.ClearContents
End If
End Sub

where the If condition has put itself on two lines, in here, again, but is
on one line in the VBA code module.

It runs fine.

Can you post exactly where your version is getting an error? What code
line, I mean.




"Ken" wrote:

If Range("a3:N2000").SpecialCells(xlCellTypeVisible). Count <
Range("a3:N2000").Cells.Count Then

Above piece of instruction lights up RED (Compile Error Expected Expression)
... I added a space and an underscore after the < at the end of the 1st line
& RED & Error message went away ... However, when I ran the Macro & the
Criteria was NOT found ... The Macro returned error message = No Cells
found???

Thanks for sticking with me ... Kha


"mark" wrote:

Ahh... good. Sorry... I wasn't sure what you meant; perhaps I didn't read
carefully enough the first time.. sorry.

Anyway, here, this will do what you need.

'***********
Selection.AutoFilter Field:=1, Criteria1:="<32*", Operator:=xlAnd
Range("A3:N2000").Select

If Range("a3:N2000").SpecialCells(xlCellTypeVisible). Count <
Range("a3:N2000").Cells.Count Then

Selection.ClearContents

End If
'************

The special cells xlcelltypevisible bit will check if the autofilter has
hidden any rows, or not, and only run the clearcontents if the two counts
(total cells vs. visible cells) don't match.



"Ken" wrote:

What I wish:

1: Criteria found ... Clear visible cells in Range A3:N2000.
2: Criteria not found ...Move on without clearing anything.

As it is:

1: Criteria found ... Clears visible cells in Range A3:N2000.
2: Criteria not found ... Clears EVERYTHING in Range A3:N2000.

Thanks ... Kha




"mark" wrote:

your code is set to clear everything left visible in the range A3:N2000 after
the application of the AutoFilter.

what do you want it to do? You didn't actually say.

If you want it to apply the filter, but not delete anything, then just take
out the two line:

Selection.ClearContents





"Ken" wrote:

Excel2003 ... I know nothing of VB Code ... I record Macros only & then
perform a little creative cut/paste as needed ... That said ... I have the
following recorded Macro instruction:

Range("A2").Select
Selection.AutoFilter Field:=1, Criteria1:="<32*", Operator:=xlAnd
Range("A3:N2000").Select
Selection.ClearContents

Issue is ... ALL Data is cleared from the Range ("A3:N2000") when the
Criteria1:="<32*" is NOT found.

When Criteria1 is found ... no problem.
When Criteria1 is not found ... it sucks to be me.

My Thanks to those that support these boards ... Kha