Solved

Macro to run from pop up window..

Posted on 2016-09-26
10
46 Views
Last Modified: 2016-09-27
Hi Experts Using Excel 2010

I have the following macro (see attached workbook) which works fine. But it's restricted in a sense that it is specific to column AC...I want to have the option to select which ever column I want...then run the macro.

Via pop up window..
test-data.xlsx
0
Comment
Question by:route217
  • 6
  • 4
10 Comments
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41816244
The file uploaded has no macros in it. To create a macro enabled file, it has to be saved as "Macro enabled" which will give it the xlsm extension.

For ease of uploading, please copy and paste the code into a snippet on a comment rather than a new file.
0
 

Author Comment

by:route217
ID: 41816253
Public Sub MyFilter()
    Dim lngStart As Long, lngEnd As Long
    lngStart = Range("B2").Value 'assume this is the start date
    lngEnd = Range("B3").Value 'assume this is the end date
   
    'assume you have field name / label in D5
    Range("AC6:AC30").AutoFilter field:=1, _
        Criteria1:=">=" & lngStart, _
        Operator:=xlAnd, _
        Criteria2:="<=" & lngEnd
End Sub

Open in new window

0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41816265
How do you want to be able to identify the column to be filtered, from a cell or a user input?

Is it coincidence that the Start Date and the date in column AC are the same or will that always be the case? If that is the case then a Find command could be used to find the correct column.
0
 

Author Comment

by:route217
ID: 41816284
User input....ignore the start date and column header
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41816297
So what will the user input to determine which column?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:route217
ID: 41816313
The Column ie AC or AD...
0
 
LVL 32

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 41816319
Try this:

Public Sub MyFilter()
    Dim lngStart As Long, lngEnd As Long
    lngStart = Range("B2").Value 'assume this is the start date
    lngEnd = Range("B3").Value 'assume this is the end date
   
    'assume you have field name / label in D5
    On Error Resume Next
    ActiveSheet.ShowAllData
    Rows("5:5").Select
    ColName = InputBox("Which column to filter?", "Column")
    Range(ColName & "5").Select
'    ColDate = Format(lngStart, "d/m/yy")
'    Selection.Find(What:=ColDate, After:=ActiveCell, LookIn:=xlValues, _
'        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
'        MatchCase:=False, SearchFormat:=False).Select
    Range(Selection, Selection.End(xlDown)).Select
    
    Selection.AutoFilter field:=1, _
        Criteria1:=">=" & lngStart, _
        Operator:=xlAnd, _
        Criteria2:="<=" & lngEnd
    ActiveCell.Offset(0, 0).Range("A1").Select
    
End Sub

Open in new window

0
 

Author Comment

by:route217
ID: 41816328
Excellent feedback
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41816367
Happy to help.
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41817427
Just found this in another question to check the user entry is valid:

ColName = InputBox("Which column to filter?", "Column")
If Not ColName Like "[A-Za-z]" Then
    MsgBox "Wrong input"
    Exit Sub
End If

This will exit the routine when a duff entry is input, the user will have to click the button again to enter another choice.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
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…
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

932 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