View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
dcdc2 dcdc2 is offline
external usenet poster
 
Posts: 4
Default Filter 'From' 'To' Dates in 1 Col

you can execute step by step to find which step is wrong and what does
exactly each one...
Here are my ideas:
FromDate = Worksheets("Sheet1").AutoFilter.Range("D5:D11")


why "FromDate = ..." ? don't you already have fromdate set ?
what was the wanted purpose of this line ?
(forget the line)

Range("D4").Select

do not need to select it, forget it too

Selection.AutoFilter

-- this is not great because you cut it if already on !
With Worksheets("Sheet1")

If not .AutoFilterMode Then
.range("D4").autofilter
end if
'this one replace the old end if . Now you are sure it's on at this step
and if you think a filter can be somewhere else:
dim a$
a="$D$4"
If .AutoFilterMode Then 'it's already on
if .autofilter.range.address < a then ' but on the wrong range
.autofiltermode = off 'cut the old one off
.range(a).autofilter
end if
else ' it was off
.range(a).autofilter 'activate it if not
end if

With .AutoFilter.Filters(1)
If .On Then FromDate = .Criteria1

it's already on ! forget that test
and you wish the opposite: .criteria1 = fromdate
(I think you do not really take care of what you are doing)

Whatever, forget all the lines from this precedent one included until this
last one (you keep it):

Selection.AutoFilter field:=1, Criteria1:="=FromDate",

Operator:=xlAnd _
, Criteria2:="<=ToDate"


which was the only usefull but is not correct:
Selection.AutoFilter field:=1, Criteria1:="<=" & FromDate,

Operator:=xlAnd _
, Criteria2:="<=" &ToDate


end with
end sub

first try this and remember to 1- exeute step by step to see what really
does each step
2- do not write ramdom code: read the visual basic help to know how works
the properties you use