Link to home
Start Free TrialLog in
Avatar of jmac001
jmac001

asked on

Reference by column name instead of an absolute number

Hi, I have a working macro that performs the following:
1. Selects all the fields in a table in the column "Functional Area" besides the  function  that was selected on the cover sheet (aka. Series_Name);
2. Deletes all those fields and their data in the table;
3. Then un-filters (shows) only the selected function

It is running fine when I refer to the column number of Initiative Summary tab (number 3). However, when I'm trying  to reference by name instead of referring to absolute column numbers, the macro doesn't work.
o      Instead of:
¿      Sheets("Initiative Summary").ListObjects("InitiativeTbl").Range.AutoFilter Field:=3
o      I try:
¿      Sheets("Initiative Summary").ListObjects("InitiativeTbl").Range.AutoFilter Field:=.ListColumns("Functional Area").Index

Could you please help me figure out how to make it work?

Sub Macro3()
'
' Macro3 Macro
'
Dim Series_Name As String

   Series_Name = Range("Cover!$C$8").Value
   
  If Series_Name <> "All" Then
   
     Sheets("Initiative Summary").ListObjects("InitiativeTbl").Range.AutoFilter Field:=3
   
     With Sheets("Initiative Summary").ListObjects("InitiativeTbl")
   
        .Range.AutoFilter Field:=.ListColumns("Functional Area").Index, Criteria1:=("<>" & Series_Name), Operator:=xlFilterValues
   
     End With
   
     Sheets("Initiative Summary").ListObjects("InitiativeTbl").ListColumns("Functional Area").DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
   
     With Sheets("Initiative Summary").ListObjects("InitiativeTbl")
        .Range.AutoFilter Field:=3
     End With
     
   Else
      Sheets("Initiative Summary").ListObjects("InitiativeTbl").Range.AutoFilter Field:=3

End If

End Sub
Avatar of andrew_man
andrew_man
Flag of Hong Kong image

Post the file here, okay?
ASKER CERTIFIED SOLUTION
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial