Solved

Hide rows where a specific word occurs.

Posted on 2011-03-06
4
328 Views
Last Modified: 2012-05-11
Dear Experts:

I would like a macro to search for the word 'column' on the active worksheet and hide the row in which this word has been found. The word 'column' could occur various times in any row.

Help is much appreciated. Thank you very much in advance for your valuable help.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
4 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35046957
Try this

Sub Sample()
    Dim i As Long, LastRow As Long
    Dim aCell As Range, bCell As Range
    Dim ExitLoop As Boolean
    Dim SearchString As String
    
    LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    SearchString = "column"
    
    For i = 1 To LastRow
        Set aCell = Sheets("Sheet1").Columns(1).Find(What:=SearchString, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        
        If Not aCell Is Nothing Then
            Set bCell = aCell
            Sheets("Sheet1").Rows(aCell.Row).EntireRow.Hidden = True
            Do While ExitLoop = False
                Set aCell = Sheets("Sheet1").Columns(1).FindNext(After:=aCell)
    
                If Not aCell Is Nothing Then
                    If aCell.Address = bCell.Address Then Exit Do
                    Sheets("Sheet1").Rows(aCell.Row).EntireRow.Hidden = True
                Else
                    ExitLoop = True
                End If
            Loop
        End If
    Next i
End Sub

Open in new window


Sid
0
 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 150 total points
ID: 35046962
Sorry. A slight amendment. Please use this. Sample File Attached.

Sid

Code Used
Sub Sample()
    Dim aCell As Range, bCell As Range
    Dim ExitLoop As Boolean
    Dim SearchString As String
    
    SearchString = "column"
    
    Set aCell = Sheets("Sheet1").Columns(1).Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                   MatchCase:=False, SearchFormat:=False)
        
    If Not aCell Is Nothing Then
        Set bCell = aCell
        Sheets("Sheet1").Rows(aCell.Row).EntireRow.Hidden = True
        Do While ExitLoop = False
            Set aCell = Sheets("Sheet1").Columns(1).FindNext(After:=aCell)
    
            If Not aCell Is Nothing Then
                If aCell.Address = bCell.Address Then Exit Do
                Sheets("Sheet1").Rows(aCell.Row).EntireRow.Hidden = True
            Else
                ExitLoop = True
            End If
        Loop
    End If
End Sub

Open in new window

HideRows.xls
0
 
LVL 14

Accepted Solution

by:
Zack Barresse earned 350 total points
ID: 35046977
You could simplify this with the Find/FindNext method...

Sub HideColumnValues()
    Dim WS As Worksheet, rCell As Range, sAddress As String
    Const sFindVal As String = "column"
    Set WS = ActiveSheet
    Set rCell = WS.Cells.Find(What:=sFindVal, LookAt:=xlPart)
    sAddress = rCell.Address
    If Not rCell Is Nothing Then
        Do
            rCell.EntireRow.Hidden = True
            Set rCell = WS.Cells.FindNext(rCell)
        Loop While Not rCell Is Nothing And rCell.Address <> sAddress
    End If
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 35058147
Dear Both:

although both codes are working, firefytr's one is more convenient to use, hence he gets the majority of the points. Anyhow, thank you very much for your great and professional help. I really appreciate it.

Regards, Andreas
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

820 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