Solved

Macro to run from pop up window..

Posted on 2016-09-26
10
43 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 31

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 31

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 31

Expert Comment

by:Rob Henson
ID: 41816297
So what will the user input to determine which column?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

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 31

Expert Comment

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

758 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

19 Experts available now in Live!

Get 1:1 Help Now