Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 60
  • Last Modified:

Macro to run from pop up window..

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
route217
Asked:
route217
  • 6
  • 4
1 Solution
 
Rob HensonIT & Database AssistantCommented:
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
 
route217Author Commented:
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
 
Rob HensonIT & Database AssistantCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
route217Author Commented:
User input....ignore the start date and column header
0
 
Rob HensonIT & Database AssistantCommented:
So what will the user input to determine which column?
0
 
route217Author Commented:
The Column ie AC or AD...
0
 
Rob HensonIT & Database AssistantCommented:
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
 
route217Author Commented:
Excellent feedback
0
 
Rob HensonIT & Database AssistantCommented:
Happy to help.
0
 
Rob HensonIT & Database AssistantCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now