Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Excel Microsoft 2007
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 9-Jul-12 21:48pm
Comments
walterhevedeich at 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?
Wes Aday at 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 at 10-Jul-12 15:02pm
   
And the issue is?
ryanb31 at 11-Jul-12 15:06pm
   
Record a macro and then you can tweak it. What have you done so far?
Sunnykumar08 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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...
  Permalink  
v2

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 13 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100