VBA: Using Table column name instead of column number

LAAdams17

Board Regular
Joined
Oct 23, 2009
Messages
73
I have the following VBA:
ActiveSheet.ListObjects("Dashboard").Range.AutoFilter Field:=64, Criteria1 _
:="Adams, Lon"

The problem is when I move columns, my VBA quits working. I'd prefer to use a table column name like this instead:
ActiveWorkbook.Worksheets("Dashboard").ListObjects("Dashboard").Sort.SortFields _
.Add Key:=Range("Dashboard[BC Owner]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal

Can someone help me rewrite the first line of code above to use the table column name instead of column number?

Thank you!
Lon
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The autofilter method requires an integer input for the field argument so you can't insert the table column name directly. Here's one way you can do you want.
Code:
Sub testit()
Dim myField As Long, myHdr As String
myHdr = "BC Owner"
With ActiveSheet.ListObjects("Dashboard").Range
    myField = .Find(myHdr).Column + 1 - .Range("A1").Column
    .AutoFilter Field:=myField, Criteria1:="Adams, Lon"
End With
End Sub
 
Upvote 0
This works perfectly for me without a ton of variable definitions needed. put the cursor in the required column header, then use ActiveCell.column to determine the number of the column ...

Range("TableName[[#Headers],[ColumnName]]").Select
ActiveSheet.ListObjects("PivotName").Range.AutoFilter Field:=ActiveCell.column, Criteria1:= Variable, Operator:=xlFilterValues
 
Upvote 0
This works perfectly for me without a ton of variable definitions needed.
.. but only if the ListObject starts in column A. ;)


Can someone help me rewrite the first line of code above to use the table column name instead of column number?
Give this a try
VBA Code:
With ActiveSheet.ListObjects("Dashboard")
  .Range.AutoFilter Field:=.ListColumns("BC Owner").Index, Criteria1:="Adams, Lon"
End With
 
Upvote 0

Forum statistics

Threads
1,214,917
Messages
6,122,233
Members
449,075
Latest member
staticfluids

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