Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
609 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
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 1000 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

971 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