Solved

Hide rows where a specific word occurs.

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

636 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