Solved

Can This Macro Be Turned into Function

Posted on 2011-03-10
10
181 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:Alex Campbell
[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:Alex Campbell
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:Alex Campbell
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:Alex Campbell
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:Alex Campbell
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:Alex Campbell
ID: 35129214
Same here. All the best.
Alex
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

717 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