Click here to Skip to main content
15,886,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a macro in excel 2007 which can filter & remove unwanted data from a range of columns and only the desired data is delievered at the end.
Posted
Comments
walterhevedeich 10-Jul-12 4:50am    
What do you mean by "and only the desired data is delievered at the end."? Do you want to copy a range of cells and then paste it on the other sheet? And by the way, what have you tried so far?
[no name] 10-Jul-12 9:23am    
"I need a macro in excel 2007"... so go ahead. I suppose that you will have to write one. But tell your supervisor that I gave you permission to write this macro.
Sandeep Mewara 10-Jul-12 15:02pm    
And the issue is?
ZurdoDev 11-Jul-12 15:06pm    
Record a macro and then you can tweak it. What have you done so far?
Sunny_Kumar_ 13-Jul-12 4:32am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.

1 solution

I think he is looking for a VBA function like SUMIF or COUNTIF but returning the entire input range with some cells blanked out (thereby filtering the input values based on a condition) You can do this with an array formula using built in functions.

Assuming your input range is A1:C10, highlight D1:F10, enter =IF(A1:C10>5,A1:C10,"") and press control-shift-enter instead of just enter. That gets you an array formula with the same size output range as the input range.

Now, assuming that is the kind of thing you want to do except you want a macro that does it so you can do something more intelligent, you can use the following:

Assuming your input range is A1:C10, then to use this macro highlight D1:F10, enter =FilterMacro(A1:C10,">",5,"") and press control-shift-enter instead of just enter. That gets you an array formula with the same size output range as the input range.

VB
Public Function FilterMacro(ByRef InputValues As Range, Operation As String, ConditionValue As Variant, EmptyValue As Variant) As Variant

    Dim Result As Variant
    Dim MatchesFilter As Boolean

    ' store all the input values
    Result = InputValues.Value2

    For CurrentRow = 1 To InputValues.Rows.Count
        For CurrentColumn = 1 To InputValues.Columns.Count
            Select Case Operation
            Case ">":  MatchesFilter = InputValues(CurrentRow, CurrentColumn) > ConditionValue
            Case "<":  MatchesFilter = InputValues(CurrentRow, CurrentColumn) < ConditionValue
            Case "<>": MatchesFilter = InputValues(CurrentRow, CurrentColumn) <> ConditionValue
            Case "=":  MatchesFilter = InputValues(CurrentRow, CurrentColumn) = ConditionValue
            End Select
            If Not MatchesFilter Then
                ' clear the ones we don't want
                Result(CurrentRow, CurrentColumn) = EmptyValue
            End If
        Next
    Next
    FilterMacro = Result
End Function


Sorry it doesn't fit on the screen so well. Perhaps I should have used shorter variable names...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900