Advanced Filter
This will do both.
Hth,
Merjet
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
|