Solved

Excel VBA hide all other rows where cell value is the same in a column when a cell is changed

Posted on 2014-10-27
4
457 Views
Last Modified: 2014-10-31
Hi

What VBA code would I use to automatically hide all rows where the value of cells in the same column
as the target cell is the same as the target cell
0
Comment
Question by:murbro
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 250 total points
Comment Utility
Hi,

Pls try

Private Sub Worksheet_Change(ByVal Target As Range)

    Set c = Target.EntireColumn.Find(Target.Value)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.EntireRow.Hidden = True
            Set c = Target.EntireColumn.FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
        Target.EntireRow.Hidden = False
    End If
End Sub

Open in new window

Regards
0
 
LVL 27

Expert Comment

by:Glenn Ray
Comment Utility
First of all, please clarify if you want this action to occur when a cell value is changed or when a cell is selected.  There is a difference in the method used.

Second, do you want the selected/changed cell to also be hidden?  If not, what is the purpose of hiding all rows but the selected/changed cell?

Third,  what method/criteria do you want to unhide all rows (reset)?  Otherwise, you'd have an ever-decreasing visible range of data and would have to manually unhide rows.

Lastly, Rgonzo's code does hide rows when a cell is changed, but it will hide all rows with even a partial substring match.  For example, if a cell is changed from "Rayford" to "Ray", all rows with cells containing the substring "Ray" would be hidden (ex., "Raymond", "Ray", "Rayford", "Rayovac").

-Glenn
0
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 250 total points
Comment Utility
The following code presumes that you want to hide rows with same value as the selected cell.  Only works when one cell in the used range is selected, otherwise all rows will be unhidden (reset).
Option Explicit
Dim valKey As Variant
Dim lngKeyRow, lngKeyCol As Long
Dim boolNoChange As Boolean
Dim rng As Range
Dim cl As Object

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If boolNoChange Then Exit Sub
    If Target.Columns.Count = 1 And Target.Rows.Count = 1 And _
        (Not Intersect(Target, UsedRange) Is Nothing) Then
        boolNoChange = True
        valKey = Target.Value
        lngKeyRow = Target.Row
        lngKeyCol = Target.Column
        Set rng = Intersect(Target.EntireColumn, UsedRange)
        For Each cl In rng
            If cl.Value = valKey Then
                cl.EntireRow.Hidden = True
            Else
                'cl.EntireRow.Hidden = False 'uncomment to only hide selected value
            End If
        Next cl
    ElseIf Intersect(Target, UsedRange) Is Nothing Then
        boolNoChange = True
        Application.ScreenUpdating = False
        Set rng = UsedRange
        For Each cl In rng
            cl.EntireRow.Hidden = False
        Next cl
        Application.ScreenUpdating = True
    End If
    boolNoChange = False
End Sub

Open in new window


As written, new rows will be hidden as cells are selected and the effect is cumulative.  If you only want to hide rows with the selected cell's value, uncomment the line noted (line 21).

Example workbook attached.

-Glenn
EE-HideDuplicates.xlsm
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

771 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

12 Experts available now in Live!

Get 1:1 Help Now