Solved

Hide rows where a specific word occurs.

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

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now