Solved

Can This Macro Be Turned into Function

Posted on 2011-03-10
10
180 Views
Last Modified: 2012-05-11
The macro below allows the filtering of a list based on the value in a cell (B2) on the queries page of the spreadsheet.

I am thinking it would look like DGET since it would be great if you give the name of the heading in the list to tell what field should be searched.

---------------------------------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        ActiveSheet.ListObjects("List1").Range.AutoFilter Field:=2, Criteria1:= _
        "=" & Range("B2"), Operator:=xlAnd
    End If
    Application.EnableEvents = True
End Sub
List-of-Books.xls
0
Comment
Question by:Alex972
[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
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35099459
function ? you mean user defined function ? no, UDF's cannot do changes or execute methods of the range object...
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35099489
Use this and you can update in A2 and B2

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Target, Range("A2:B2")) Is Nothing Then
        If Len(Target) = 0 Then
            ActiveSheet.ListObjects("List1").Range.AutoFilter
        Else
            ActiveSheet.ListObjects("List1").Range.AutoFilter Field:=Target.Column, Criteria1:= _
                "=" & Target, Operator:=xlAnd
        End If
    End If
    Application.EnableEvents = True
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Alex972
ID: 35108786
I guess I am asking for it be interactive if UDFs don't work.
I don't want to have edit the macro if the cells involved are not the ones in the macro.
Can the macro ask for the location of the query field title and the query value?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Alex972
ID: 35128779
While this macro does allow me to choose more than one column to query on, I was looking for a way to tell it a particular column by number of the column or the column name in the list to use for the query:

"I am thinking it would look like DGET since it would be great *if you give the name of the heading in the list to tell what field should be searched*. "

0
 
LVL 39

Expert Comment

by:nutsch
ID: 35128941
So if you had one field for column name and one field for filter value, that would work for you?

T
0
 
LVL 1

Author Comment

by:Alex972
ID: 35129025
That's it exactly!
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 35129068
Check out the attached file on the queries tab and see if that works. Pick column in dropdown cell a1, enter the filter in cell b1.

Thomas
List-of-Books-3-.xls
0
 
LVL 1

Author Closing Comment

by:Alex972
ID: 35129111
Very useful!!
Gives me the flexibility I was looking for.  I was interested in this because I get lists where the data I am searching for may be in wrong column. This is great!!
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35129161
Glad we were able to figure out what you needed. Thanks for the grade.

Thomas
0
 
LVL 1

Author Comment

by:Alex972
ID: 35129214
Same here. All the best.
Alex
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

749 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