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
482 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 49

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

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

939 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