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
561 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 52

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:Murray Brown
ID: 40416144
Thanks very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

632 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