When you .AutoFilter on a range, you have to tell the filter what field (column) to filter on. The field numbers start at 1 where 1 is the first column in the range. So if the range is from V:Z, then field 1 = column V.
Knowing that, let's look at that code:
- RptPrdCol is a column letter, which in the latest case is column AD
- CCMPatCol is a column letter, which in the latest case is column Y
- RngRecords is a Range object, which is: Columns(RptPrdCol & ":" & CCMPatCol), or in other words Columns("AD:Y") which = Columns("Y:AD") (which is how it would normally be read)
Knowing the definitions of those variables, we can understand the calculation happening:- The range is from Y to AD. That's a total of 6 columns including Y and AD
- When asking for RngRecords.Column, it will only give the first column, Y, which is 25.
- So columns(AD).column (30) - RngRecords.Column (25) + 1 = 6, which is the correct field number for the .AutoFilter that will be applied later
Bookmarks