我正在尝试构建费用数据库和报告生成器,以便我的公司可以查看和跟踪指定范围内的费用以及这三个过滤器:"员工","客户端"和"类别"(费用类别)。除了我尝试使用多个过滤器时,一切正常。
如果我没有使用过滤器,那么他们全部=""它很棒。如果我只使用"员工"过滤它工作得很好,但如果我尝试使用员工过滤器与其他2个过滤器或其他1个过滤器,则会错误地指出它无法找到开始日期或结束日期。不确定发生了什么。
Private Function GetReportInformationFromExpensesMaster(ByVal reportStartDate As Date, ByVal reportEndDate As Date, Optional ByVal employee As String, Optional ByVal client As String, Optional ByVal category As String) As Boolean
'Generates report on reports master
Dim transferDataRange As Range
Dim startDateRow As Integer, endDateRow As Integer
Dim errorMessage As String
'employee filter
If employee <> "" Then ExpenseMasterList.Range(ExpenseMasterList.Cells(2, MASTERLIST_EMPLOYEE_COLUMN), ExpenseMasterList.Cells(FindLastRow(ExpenseMasterList, MASTERLIST_EMPLOYEE_COLUMN), MASTERLIST_EMPLOYEE_COLUMN)).AutoFilter _
Field:=1, _
Criteria1:=employee, VisibleDropDown:=False
'client filter
If client <> "" Then ExpenseMasterList.Range(ExpenseMasterList.Cells(2, MASTERLIST_RELATED_CLIENT_COLUMN), ExpenseMasterList.Cells(FindLastRow(ExpenseMasterList, MASTERLIST_RELATED_CLIENT_COLUMN), MASTERLIST_RELATED_CLIENT_COLUMN)).AutoFilter _
Field:=1, _
Criteria1:=client, VisibleDropDown:=False
'Category
If category <> "" Then ExpenseMasterList.Range(ExpenseMasterList.Cells(2, MASTERLIST_CATEGORY_COLUMN), ExpenseMasterList.Cells(FindLastRow(ExpenseMasterList, MASTERLIST_CATEGORY_COLUMN), MASTERLIST_CATEGORY_COLUMN)).AutoFilter _
Field:=1, _
Criteria1:=category, VisibleDropDown:=False
'Find rows after everything sorted
startDateRow = FindDateRow(reportStartDate, True)
endDateRow = FindDateRow(reportEndDate, False)
If startDateRow = -1 Then
errorMessage = "Start Date not found in Expense Master under these parameters"
GetReportInformationFromExpensesMaster = True
GoTo ErrorOutGetReport
ExpenseMasterList.AutoFilterMode = False
ElseIf endDateRow = -1 Then
errorMessage = "End date not found in Expense Master under these parameters"
GetReportInformationFromExpensesMaster = True
GoTo ErrorOutGetReport
ExpenseMasterList.AutoFilterMode = False
End If
Set transferDataRange = ExpenseMasterList.Range(ExpenseMasterList.Cells(startDateRow, MASTERLIST_REF_ID_COLUMN), ExpenseMasterList.Cells(endDateRow, MASTERLIST_USD_AMOUNT_COLUMN))
transferDataRange.Copy
ReportSheet.Cells(9, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Clears autofilter mode on expense master list if necessary
ExpenseMasterList.AutoFilterMode = False
ReportSheet.Cells(8, 1).Activate
Exit Function
ErrorOutGetReport:
MsgBox (errorMessage)
End Function
按日期查找开始/结束行
Public Function FindDateRow(ByVal dateToBeFound As Date, ByVal startDateBoolean As Boolean) As Integer
'Finds date locations and if it is unable to find said date then it will return 0 to indicate that a suitable date was not found
Dim finderRange As Range, masterListDateRange As Range
Dim masterListLastRow As Integer, dateRowFound As Integer
Dim dateLoopProxy As Date
masterListLastRow = FindLastRow(ExpenseMasterList, MASTERLIST_DATE_COLUMN)
Set masterListDateRange = ExpenseMasterList.Range("B3:B" & masterListLastRow)
'Startdate or end date
If startDateBoolean Then
'StartDate
Set finderRange = masterListDateRange.Find(dateToBeFound, after:=Cells(3, MASTERLIST_DATE_COLUMN), searchdirection:=xlNext)
If finderRange Is Nothing Then
'If date is not found iterate forward until it is found
'Date proxy to test initial month against proxy month for iteration through the do loop
dateLoopProxy = dateToBeFound
Do Until Not finderRange Is Nothing Or Month(dateLoopProxy) <> Month(dateToBeFound)
dateLoopProxy = dateLoopProxy + 1
Set finderRange = masterListDateRange.Find(dateLoopProxy, after:=Cells(3, MASTERLIST_DATE_COLUMN), searchdirection:=xlNext)
Loop
End If
If Not finderRange Is Nothing Then dateRowFound = finderRange.Row Else GoTo DateNotFound
Else
'End Date
Set finderRange = masterListDateRange.Find(dateToBeFound, after:=Cells(masterListLastRow, MASTERLIST_DATE_COLUMN), searchdirection:=xlPrevious)
If finderRange Is Nothing Then
'If date is not found iterate backwards until it is found
'Date proxy to test initial month against proxy month for iteration through thedo loop
dateLoopProxy = dateToBeFound
Do Until Not finderRange Is Nothing Or Month(dateLoopProxy) <> Month(dateToBeFound)
'Iterating backwards (up) to find end date
dateLoopProxy = dateLoopProxy - 1
Set finderRange = masterListDateRange.Find(dateLoopProxy, searchdirection:=xlPrevious)
Loop
End If
If Not finderRange Is Nothing Then dateRowFound = finderRange.Row Else GoTo DateNotFound
End If
FindDateRow = dateRowFound
Exit Function
DateNotFound:
FindDateRow = -1
End Function