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
523 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 50

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 40407803
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
ID: 40409149
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
ID: 40409367
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
ID: 40416144
Thanks very much
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

808 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