VBA - Find and filter last column

leatherhen99

New Member
Joined
Dec 17, 2019
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Good afternoon and a happy new year!

I'm creating a macro that will look for the column and filter... the only problem is, there could be 14 columns one run and there could be 12 the next (it could range from more or less), but it'll always be in the last column... but when I create the code, it always has "field:=8"... but that's not always going to be the last column... I haven't found any workarounds for this... can anyone help???

These are the two that I've tried...but if the field number changes, how do i get the macro to select the correct field to filter???

Within a table:
Sub lastcol()
'
' lastcol Macro
ActiveCell.Offset(-14, 0).Range("A1").Select
Selection.End(xlToRight).Select
ActiveSheet.ListObjects("CalEvents").Range.AutoFilter Field:=8, Criteria1:= _
"0:00:00"
End Sub

within a range:
Sub Filter_Last()
' Filter_Last Macro
'
ActiveCell.Offset(-11, -2).Range("A1").Select
Selection.End(xlToRight).Select
Selection.AutoFilter
ActiveSheet.Range("$A$3:$H$29").AutoFilter Field:=8, Criteria1:="0:00:00"
End Sub

Thanks!
Heather
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How about
VBA Code:
   With ActiveSheet.ListObjects("CalEvents")
      .Range.AutoFilter .ListColumns.Count, "0:00:00"
   End With
 
Upvote 0
You can do it like this:
VBA Code:
Dim ans As Long
ans = ActiveSheet.ListObjects("CalEvents").ListColumns.Count
ActiveSheet.ListObjects("CalEvents").Range.AutoFilter Field:=ans, Criteria1:="0:00:00"
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top