Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 06-18-2009, 06:09 PM
dcgrove dcgrove is offline
Forum Contributor
 
Join Date: 16 Feb 2008
Location: Mansfield, TX
Posts: 196
dcgrove is becoming part of the community
Modify code to hide rows based on the contents in a cell.

Please Register to Remove these Ads

Hello, I am using the code below to hide all rows with the text "Solar" in the specified range. How can I modify this so that instead of looking for "Solar" it can be set to look in a cell that will be a data validation list. My goal is for the user to be able to show only the rows that have the word they selected from the list. I would like for it to change when ever the word is clicked on in the list if possible. I would also like for their to be an option to show all rows if the word "All" is selected.

If there is a faster way to accomplish what I am doing please let me know. As it is the code slows down my computer pretty badly.

Thanks!
Clayton

Code:
Sub HideRows()
 Dim R As Long
 Dim Rng As Range
    Set Rng = Worksheets("excel").Range("b7:b900")
   
     For R = Rng.Rows.Count To 1 Step -1
       If Not Rng.Cells(R, 1) = "Solar" Then Rng.Cells(R, 1).EntireRow.Hidden = True
     Next R
End Sub
Reply With Quote
  #2  
Old 06-18-2009, 07:29 PM
Leith Ross's Avatar
Leith Ross Leith Ross is offline
Forum Moderator
 
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,486
Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding
Send a message via AIM to Leith Ross
Re: Modify code to hide rows based on the contents in a cell.

Hello Clayton,

When you need to check a large number of cells, it is faster to store the cell values in a Variant Array. Also, using the Like operator is faster way to search for and match text in a string.
Code:
Sub HideRows()

  Dim R As Long
  Dim Rng As Range
  Dim X As Variant
  
    Set Rng = Worksheets("excel").Range("B7:B900")
    X = Rng.Value
    
      For R = UBound(X, 1) To 1 Step -1
        If LCase(X(R, 1)) Like "*solar*" Then
           Rng.Rows(R).Hidden = True
        End If
      Next R

End sub
__________________
Sincerely,
Leith Ross

Remember To Do the Following....

1. Use code tags. Place [code] before the first line of code and [/code] after the last line of code.
2. Thank those who have helped you by Clicking the scales above each post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.


Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Reply With Quote
  #3  
Old 06-18-2009, 09:07 PM
dcgrove dcgrove is offline
Forum Contributor
 
Join Date: 16 Feb 2008
Location: Mansfield, TX
Posts: 196
dcgrove is becoming part of the community
Re: Modify code to hide rows based on the contents in a cell.

Thanks Leith! How would I make this search the contents of one particular cell instead of the word "Solar"? My goal is to have a data validation list that the user selects a word from and the macro will hide all rows without that word in them. You could use cell H3 as the cell the list is in if you need to.


Thanks!
Clayton

Last edited by dcgrove; 06-19-2009 at 06:13 PM.
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump