Thread: Advanced Filter
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Chris Hankin[_3_] Chris Hankin[_3_] is offline
external usenet poster
 
Posts: 20
Default Advanced Filter

Hello Merjet,

I used your VBA code you provided and unfortunately ran into a slight
snag. You code was:

Sub Query()
Dim dtStart As Date
Dim dtEnd As Date
Dim aCode As String
Dim iEnd As Long
Dim dSum As Double
Dim c As Range
Dim rng As Range
Dim ws As Worksheet

Set ws = Sheets("DPC Expenses")
ws.Activate
iEnd = ws.Range("B2").End(xlDown).Row
Set rng = ws.Range("B2:B" & iEnd)
dtStart = InputBox("Enter start date (mm/dd/yy).")
dtEnd = InputBox("Enter end date (mm/dd/yy).")
aCode = InputBox("Enter code. Leave blank for all.")
For Each c In rng
If c = dtStart And c <= dtEnd Then
If aCode = "" Then dSum = dSum + c.Offset(0, 7)
If aCode = c.Offset(0, 9) Then dSum = dSum + c.Offset(0,7)
End If
Next c
Sheets("Query").Range("A2") = dtStart
Sheets("Query").Range("B2") = dtEnd
If aCode = "" Then
Sheets("Query").Range("C2") = "all"
Else
Sheets("Query").Range("C2") = aCode
End If
Sheets("Query").Range("D2") = dSum
Sheets("Query").Activate
End Sub


I tried to use the code to calculate the total cost between the dtStart:
08/01/06 (01 Aug 06) and dtEnd: 08/30/06 (30 Aug 06) for aCode: 21008.
The code summed the costs for July and August. I then changed the
following code from:

iEnd = ws.Range("B2").End(xlDown).Row
Set rng = ws.Range("B2:B" & iEnd) to:

iEnd = ws.Range("B8").End(xlDown).Row
Set rng = ws.Range("B8:B" & iEnd).

The code then successfully calculated the costs.

However, this only worked for August 2006.

My worksheet: DPC Expenses has a header row at row 2 and the data starts
at row 3. Row 1 is blank at the moment - but it will be used for macro
buttons.

Could you please refine your code to make it work better?

Thanks again for all your help and I sincerely hope that you may be able
to help again.

Many thanks,

Chris.

Live Long and Prosper :-)

*** Sent via Developersdex http://www.developersdex.com ***