Solved

Macro to run from pop up window..

Posted on 2016-09-26
10
53 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
[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
  • 6
  • 4
10 Comments
 
LVL 33

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 33

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

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

Expert Comment

by:Rob Henson
ID: 41816297
So what will the user input to determine which column?
0
 

Author Comment

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

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 33

Expert Comment

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

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

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

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

739 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