Solved

Hide rows where a specific word occurs.

Posted on 2011-03-06
4
325 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
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…
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…

760 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

16 Experts available now in Live!

Get 1:1 Help Now