Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default code to hide rows based on criteria - but without looping

Thanks Matt, I had only tried your first one, not your second one. This
second one with the Union works great and is very fast.

Thank you for introducing me to Union, and for your kind help as well. Now
I'm off and running.

Have a great day!
Harold


"Matthew Herbert" wrote in
message ...
Harold,

I created dummy values in column E and input an IF function in column F
(e.g. =IF(E9="","",E9)). When I used the For Each loop (listed below), I
didn't have any trouble hiding the rows. Test this code and see if you
get
the same results; otherwise, you might need to provide more details
regarding
your function, whether rows are already hidden, etc.

Best,

Matt

Sub TestHideRows()
Dim rngEval As Range
Dim rngHide As Range
Dim rngCell As Range

Set rngEval = ActiveSheet.Range("F9:F98")

For Each rngCell In rngEval.Cells
If rngCell.Value = "" Then
If rngHide Is Nothing Then
Set rngHide = rngCell
Else
Set rngHide = Union(rngHide, rngCell)
End If
End If
Next rngCell

Debug.Print rngHide.Address
rngHide.EntireRow.Hidden = True

End Sub

"Harold Good" wrote:

Hi, I have tried this and the other suggestion offered by Matthew
Herbert.
Neither seem to work and I think the problem might be that all cells in
this
F range have formulas in them. In the ones I want to hide, the formulas
solve to "" so they appear blank to the user.

But it appears that SpecialCells(xlCellTypeBlanks) does not count these
as
blank cells. If I delete the formula then the code below works.

Is there a way to make this work so that if any cells in this range = "",
they will be hidden?

Thanks again for your help,
Harold


"Don Guillett" wrote in message
...
How about a nice one liner
Sub deleteblankrows()
Range("a1:a6").SpecialCells(xlCellTypeBlanks).Enti reRow.hidden=true
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Harold Good" wrote in message
...
Hi,

I'd like to hide unused rows in a budget form based on a formula in
Col F
of any row in the range below that is equal to "". I know little
about
VBA, but the code below works, thhough it is too slow to do everytime
it
recalculates. Because Column F values are entered from a separate
worksheet, I cannot use the Worksheet_Change Event.

Private Sub Worksheet_Calculate()
Dim cell As Range
Application.EnableEvents = False
For Each cell In Range("F9:F98")
If cell.Value = "" Then
cell.EntireRow.Hidden = True
End If
Next cell
Application.EnableEvents = True
End Sub

What I've read elsewhere is to avoid Loops whenever possible. Since
all
the rows that do not equal "" are at the top, and all those that equal
""
are at the bottom, is there a better way to do this using CountA, or
SpecialCells?

Thanks for any help you can offer,
Harold







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code for Excel 2007 to hide rows based on sum of several rows not ina range Joe Gardill Excel Programming 2 August 29th 08 03:53 PM
Macro to hide rows based on criteria Santa-D Excel Programming 5 July 1st 08 09:08 AM
Code help, delete rows based on column criteria Stout Excel Discussion (Misc queries) 2 March 20th 07 01:17 PM
Code to hide Entire row based on criteria ram Excel Programming 7 February 9th 06 09:31 PM
Code to hide rows based on cell contents Tim[_29_] Excel Programming 5 December 17th 03 02:59 PM


All times are GMT +1. The time now is 09:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"