PDA

View Full Version : Autofilter failed



Programs1234
08-15-2017, 10:28 AM
For some reason my code fails at the last line here:


Sub credit_monitor()

Dim FileName1 As String
Dim LR As Long
Dim TaxRate As String
Dim SumAllCountries As Variant
Dim LR2 As Variant
Dim LR3 As Variant
Dim xdate As String
Dim fname As Variant
Dim fname2 As Variant
Dim NextBusinessDay As Variant



FileName1 = Workbooks("Macros").Sheets(1).Cells(7, 2)

Workbooks(FileName1).Activate

Workbooks(FileName1).Sheets(1).Select

xdate = Cells(8, 2)

FR = Range("A" & Rows.Count).End(xlUp)

NextBusinessDay = Workbooks("Macros").Sheets(1).Cells(9, 2)

'Fix Tax Rate to 100

ActiveSheet.Range("A1:Q1").AutoFilter Field:=17, Criteria1:="FIM"

LR = Range("A" & Rows.Count).End(xlUp).Row

Range("K2:K" & LR).SpecialCells(xlCellTypeVisible).Select

TaxRate = "100"

Range("K2:K" & LR).SpecialCells(xlCellTypeVisible) = TaxRate

Range("A1:Q1").AutoFilter






'Sort End Date for Blank spaces and replace with 12/31/2099

ActiveSheet.Cells(1, 15).AutoFilter Field:=15, Criteria1:=""

'Range("O2:O" & LR).SpecialCells(xlCellTypeVisible) = "12/31/2099"

Range("A1:Q1").AutoFilter




'Sort End Date by tomorrow and finds and returns sum of collateral levels to macros sheet





ActiveSheet.Cells(1, 15).AutoFilter Field:=15, Criteria1:=NextBusinessDay, Operator:=xlFilterDynamic <-------- This is where it fails


If anybody can help, that'd be great.

p45cal
08-15-2017, 11:46 AM
What's in the NextBusinessDay variable (=What's in Workbooks("Macros").Sheets(1).Cells(9, 2))?
With xlFilterDynamic you'd expect criteria1 to be one of:
xlFilterToday
xlFilterYesterday
xlFilterTomorrow
xlFilterThisWeek
and 30 or so others (or their equivalent numbers).

Programs1234
08-15-2017, 11:57 AM
I just removed the Operator:=xlFilterDynamic and it started working again for some reason. NextBusinessDay just means the date for the next business day. I want to filter data for the next business day. The reason why I cannot put down xlTomorrow is because if its a friday, then i would end up sorting for saturday. And my documents do not contain info regarding saturdays or sundays. I still think the next business day thing is kind of annoying. Is there a way to drop Saturday and Sunday and have the days go from Monday through Friday then back to Monday?

p45cal
08-15-2017, 12:09 PM
You should be able to do one of the following (untested):
ActiveSheet.Cells(1, 15).AutoFilter Field:=15, Criteria1:=Application.WorkDay_Intl(Date, 1)
ActiveSheet.Cells(1, 15).AutoFilter Field:=15, Criteria1:=[WORKDAY.INTL(TODAY(),1)]