Solved

Can This Macro Be Turned into Function

Posted on 2011-03-10
10
176 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
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:FernandoFernandes
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Alex972
Comment Utility
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
 

Author Comment

by:Alex972
Comment Utility
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
Comment Utility
So if you had one field for column name and one field for filter value, that would work for you?

T
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Alex972
Comment Utility
That's it exactly!
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:Alex972
Comment Utility
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
Comment Utility
Glad we were able to figure out what you needed. Thanks for the grade.

Thomas
0
 

Author Comment

by:Alex972
Comment Utility
Same here. All the best.
Alex
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 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

16 Experts available now in Live!

Get 1:1 Help Now