?
Solved

Excel Function

Posted on 2011-09-27
7
Medium Priority
?
211 Views
Last Modified: 2012-05-12
I require an excel function that can do the following.

Scan a sheet that I specifiy in the VBA in code an filter the data currently sheet by looking through specific columns that I specify and criteria that I can specify.  For example:

Sheet = DataSheet
Column A = >1000
Column C - Must Contain the following string ("google")
Column E <> 12345 Or 54321
Column F = "marketshare"

The rows that meet that criteria are to removed from the datasheet.

Please keep function as generic as possible will would like to use filter through multiple datasheets all of have varing amounts of columns.

Commenting would be cool, cya.
0
Comment
Question by:Zack
7 Comments
 
LVL 50
ID: 36708494
Hello,

what code have you come up with so far? Where did you get stuck? Which aspect of the VBA do you need help with?

Can you post your code?

0
 
LVL 5

Expert Comment

by:DerZauberer
ID: 36708496
Uhm isn't that a standard excel function?

Select whole sheet, use the normal Data/Filter with maybe userdefined criterias for the columns you want, then delete resulting rows... done...

If you need VBA code, record a macro for that.
0
 

Author Comment

by:Zack
ID: 36708661
Sure this what I have so far.

Sub RowFilterDeletion()
    Dim startrow As Long
    Dim endrow As Long
    Dim currentrow As Long
 
    With ActiveSheet

        startrow = 10
        endrow = 50

        For currentrow = endrow To startrow
           
            With .Cells(currentrow, "A")

                If Not IsError(.Value) Then

                    If .Value = "1000" Then .EntireRow.Delete            

                End If

            End With

        Next currentrow

    End With

End Sub

Not to sure about adding additional criteria that I am after.
Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 34

Expert Comment

by:Rob Henson
ID: 36708725
Using the standard AutoFilter, depending on the order of the selection of criteria; it would filter for criteria1 AND criteria2 AND criteria3 etc etc.

You could use the Advanced Filter (AF) function to copy the data you want to keep to a different sheet, thus leaving the original data intact, to be deleted if so required.

The criteria for AF is specified in a separate table, using column headers from the data. Where two items of criteria are placed on the same row within this table, the filter takes it as criteria1 AND criteria2. If placed on separate rows, it treats it as criteria1 OR criteria2.

So to get an AND style comparison your criteria table would be:

Col A      Col C         Col E         Col F
>1000    *google*  <>12345   marketshare
>1000    *google*  <>54321   marketshare

To get an OR comparison:

Col A      Col C         Col E         Col F
>1000    
              *google*  
                               <>12345
                               <>54321
                                                 marketshare

The Col A, C, E & F references will have to be replaced with the column headers from the data. If necessary these could be linked to the data headers as dropdowns so that the user can select the column header for which they want to do a filter.

The AF routine from the Data menu would then need 3 references to function:

Ref 1:  Data Area
Ref 2:  Criteria area
Ref 3:  Destination for copied data, if you want all columns from the data make this area the same number of columns wide as the original.

Thanks
Rob H
0
 
LVL 34

Expert Comment

by:Rob Henson
ID: 36708763
Looking at it, you want to removce the data with those criteria or in other words keep the data that does not meet those criteria.

You would have to change the criteria:

<=1000
<>*google*
=12345
=54321
<>marketshare

Thanks
Rob H
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 2000 total points
ID: 36708918
Your code is a reasonable start.  If you want to do it this way rather than using a filter, which can cause problems with screen display when running vba code, then you could add a match function where you define your criteria.  I prefer to do it this way as it's easier to see what's going on, and easier for you to change later to add more citeria, or change the existing ones.  This isn't exhaustively tested but I think it is pretty much right:

Sub RowFilterDeletion()
    Dim startrow As Long
    Dim endrow As Long
    Dim currentrow As Long
 
    With ActiveSheet

        startrow = 10
        endrow = 14

        For currentrow = endrow To startrow Step -1
            If FilterMatch(.Rows(currentrow)) Then
                .Rows(currentrow).EntireRow.Delete
            End If
        Next currentrow
    End With
End Sub

Private Function FilterMatch(ByRef rw As Excel.Range) As Boolean

'Column A >= 1000
'Column C - Must Contain the following string ("google")
'Column E <> 12345 Or 54321
'Column F = "marketshare"

    FilterMatch = False
    
    If IsError(rw.Cells(1, 1).Value) Then Exit Function
    If Not (rw.Cells(1, 1).Value >= 1000) Then Exit Function
    If Not (rw.Cells(1, 3).Value = "google") Then Exit Function
    If Not ((rw.Cells(1, 5).Value = 12345) Or (rw.Cells(1, 5).Value = 54321)) Then Exit Function
    If Not (rw.Cells(1, 6).Value = "marketshare") Then Exit Function
    
    FilterMatch = True
    
End Function

Open in new window

As you can see, the function quits, returning False as soon as one does not match, so you don't waste time checking additional criteria. The other strength of this approach is that you can make your criteria quite complex in ways that would difficult to do with built-in filtering.
0
 

Author Closing Comment

by:Zack
ID: 36709101
Thank you for your assistance.

I be sure to post my starting code in the future I didn't mean to offend anyone.
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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 will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

750 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