Solved

Macro to run from pop up window..

Posted on 2016-09-26
10
55 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

630 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