Solved

Reference by column name instead of an absolute number

Posted on 2013-12-05
2
340 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:jmac001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Expert Comment

by:andrew_man
ID: 39699023
Post the file here, okay?
0
 
LVL 33

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 39700495
Does the user have to select the column name in some way?

If so, create a variable from that selection and use that variable in a MATCH function to generate the column number:

Application.WorksheetFunction.Match(Variable,Range,MatchType)

Thanks
Rob H
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question