Solved

Reference by column name instead of an absolute number

Posted on 2013-12-05
2
319 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
2 Comments
 
LVL 4

Expert Comment

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now