Solved

EXCEL example needed

Posted on 2013-05-14
2
267 Views
Last Modified: 2013-05-14
I need an EXCEL VBA example of filtering a spreadsheet without using the built in filter function.

At the top of the worksheet is a dropdown with company names (
Company A, Company B, Company C.)

On the spreadsheet is a list of rows.
Company A      Winget 1
Company B      Winget 1
Company A      Winget 3
Company A      Winget 4
Company A      Winget 5
Company B      Winget 3
Company C      Winget 3
Company C      Winget 4

When a name is selected only the rows with the company name shows.
If you select Company A you will ony see these rows.
Company A      Winget 1
Company A      Winget 3
Company A      Winget 4
Company A      Winget 5
0
Comment
Question by:Idarac
2 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39165604
This code inserted into Sheet 1 (where sheet1 has the data you provided above) will do it:

Note: This solution does not offer the user an obvious way to "Unfilter" the rows

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim selectedValue As String
    Dim thisCell As Range
    Dim myLastCell As Range
    
    
    If Target.Column = 1 Then
        
        ' Need to make sure that we don't go past the end of the used portion of the sheet
        Set myLastCell = LastCell(ActiveSheet)

       ' Loop through each used cell in column 1
        
        For Each thisCell In Range("A1:A" & myLastCell.Row)

           'Set the entire row to hidden if it does not match the currently selected cell

            If thisCell.Value <> Target.Value Then
                thisCell.EntireRow.Hidden = True
            Else
                thisCell.EntireRow.Hidden = False
            End If
        Next
    
    End If
    
End Sub

Function LastCell(ws As Worksheet) As Range
'
' Note "&" denotes a long value; "%" denotes an integer value
  
  Dim LastRow&, lastCol%

' Error-handling is here in case there is not any
' data in the worksheet

  On Error Resume Next

  With ws

  ' Find the last real row

    LastRow& = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row

  ' Find the last real column

    lastCol% = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column

  End With

' Finally, initialize a Range object variable for
' the last populated row.

  Set LastCell = ws.Cells(LastRow&, lastCol%)

End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:Idarac
ID: 39166156
Great example thank you very much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now