Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hide rows where a specific word occurs.

Posted on 2011-03-06
4
Medium Priority
?
334 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 600 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 1400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

721 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