PC Review


Reply
Thread Tools Rate Thread

code to hide rows based on criteria - but without looping

 
 
Harold Good
Guest
Posts: n/a
 
      25th Aug 2009
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 With Quote
 
 
 
 
Matthew Herbert
Guest
Posts: n/a
 
      25th Aug 2009
Harold,

I realized I wrote the code to delete rows, not to hide rows. I apologize
for not reading your post carefully enough. Simply exchange the
..EntrieRow.Delete with .EntrieRow.Hidden = True.

Best,

Matt

"Harold Good" wrote:

> 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 With Quote
 
Matthew Herbert
Guest
Posts: n/a
 
      25th Aug 2009
Harold,

I've included the two separate ways below that you mention. In general,
performing methods on an aggregate basis rather than a one-by-one basis is
fastest, thus, you'll see the Union function below. In your situation, when
you delete one-by-one, Excel may be recalculating after every deletion, so
you may have up to 90 different recalculations on a one-by-one basis rather
than 1 recalculation on the aggregate. You can loop one-by-one if you so
desire because a loop of 90 items is practically negligible. (Also, I would
comment the .EntireRow.Delete syntax below and uncomment the Debug.Print
lines. Debug.Print will print to the Immediate Window (View | Immediate
Window) and will allow you to see how the program is behaving prior to
executing a deletion).

Best,

Matthew Herbert

Sub TestDeleteBlanks()
Dim rngEval As Range
Dim rngDelete As Range
Dim rngCell As Range

Set rngEval = ActiveSheet.Range("F9:F98")
Set rngDelete = rngEval.SpecialCells(xlCellTypeBlanks)
'Debug.Print rngDelete.Address
rngDelete.EntireRow.Delete

Set rngDelete = Nothing

'-OR-

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

'Debug.Print rngDelete.Address
rngDelete.EntireRow.Delete

End Sub

"Harold Good" wrote:

> 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 With Quote
 
Don Guillett
Guest
Posts: n/a
 
      26th Aug 2009
How about a nice one liner
Sub deleteblankrows()
Range("a1:a6").SpecialCells(xlCellTypeBlanks).EntireRow.hidden=true
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Harold Good" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 With Quote
 
Harold Good
Guest
Posts: n/a
 
      26th Aug 2009
Thanks very much Matthew, I shall give this a try tomorrow!
Harold


"Matthew Herbert" <(E-Mail Removed)> wrote in
message news:A2F52998-0B6B-40B6-A562-(E-Mail Removed)...
> Harold,
>
> I realized I wrote the code to delete rows, not to hide rows. I apologize
> for not reading your post carefully enough. Simply exchange the
> .EntrieRow.Delete with .EntrieRow.Hidden = True.
>
> Best,
>
> Matt
>
> "Harold Good" wrote:
>
>> 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 With Quote
 
Harold Good
Guest
Posts: n/a
 
      26th Aug 2009
Thank you Don, I will try this out. So simple I need to study it to see what
all it's doing!

Harold


"Don Guillett" <(E-Mail Removed)> wrote in message
news:u$(E-Mail Removed)...
> How about a nice one liner
> Sub deleteblankrows()
> Range("a1:a6").SpecialCells(xlCellTypeBlanks).EntireRow.hidden=true
> End Sub
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "Harold Good" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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 With Quote
 
Don Guillett
Guest
Posts: n/a
 
      26th Aug 2009
Just look in the vba help index for SPECIALCELLS

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Harold Good" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Thank you Don, I will try this out. So simple I need to study it to see
> what all it's doing!
>
> Harold
>
>
> "Don Guillett" <(E-Mail Removed)> wrote in message
> news:u$(E-Mail Removed)...
>> How about a nice one liner
>> Sub deleteblankrows()
>> Range("a1:a6").SpecialCells(xlCellTypeBlanks).EntireRow.hidden=true
>> End Sub
>> --
>> Don Guillett
>> Microsoft MVP Excel
>> SalesAid Software
>> (E-Mail Removed)
>> "Harold Good" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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 With Quote
 
K_Macd
Guest
Posts: n/a
 
      26th Aug 2009
Much better to have collective operations rather than loops.

Also when running any macro working with a large data set or complex
calculations consider setting the following off then resetting on after
completion.

Application.ScreenUpdating = False/True
Application.Calculation = xlCalculationManual/xlCalculationAutomatic
Application.EnableEvents = False/True

The most significant of these is the first with only incremental gains from
the others.

--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Don Guillett" wrote:

> Just look in the vba help index for SPECIALCELLS
>
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "Harold Good" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Thank you Don, I will try this out. So simple I need to study it to see
> > what all it's doing!
> >
> > Harold
> >
> >
> > "Don Guillett" <(E-Mail Removed)> wrote in message
> > news:u$(E-Mail Removed)...
> >> How about a nice one liner
> >> Sub deleteblankrows()
> >> Range("a1:a6").SpecialCells(xlCellTypeBlanks).EntireRow.hidden=true
> >> End Sub
> >> --
> >> Don Guillett
> >> Microsoft MVP Excel
> >> SalesAid Software
> >> (E-Mail Removed)
> >> "Harold Good" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >>> 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 With Quote
 
Harold Good
Guest
Posts: n/a
 
      26th Aug 2009
Thanks Ken for this great reminder. I knew about the screen updating but
thought it was only to make it look nicer during the process. I had no idea
it'd impact speed. I did a rough timing of it to go thru the 90 cells, it
took about 4 times longer with updating = true.

Great suggestion.
Harold


"K_Macd" <kmacdonald "A_T" activ8 ''''''''''''''''''''''''''''''''D O
T'''''''''''''''''''''''''''''''' net [S> wrote in message
newsE860EEF-14D0-4E8B-9580-(E-Mail Removed)...
> Much better to have collective operations rather than loops.
>
> Also when running any macro working with a large data set or complex
> calculations consider setting the following off then resetting on after
> completion.
>
> Application.ScreenUpdating = False/True
> Application.Calculation =
> xlCalculationManual/xlCalculationAutomatic
> Application.EnableEvents = False/True
>
> The most significant of these is the first with only incremental gains
> from
> the others.
>
> --
> Ken
> "Using Dbase dialects since 82"
> "Started with Visicalc in the same year"
>
>
> "Don Guillett" wrote:
>
>> Just look in the vba help index for SPECIALCELLS
>>
>> --
>> Don Guillett
>> Microsoft MVP Excel
>> SalesAid Software
>> (E-Mail Removed)
>> "Harold Good" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>> > Thank you Don, I will try this out. So simple I need to study it to see
>> > what all it's doing!
>> >
>> > Harold
>> >
>> >
>> > "Don Guillett" <(E-Mail Removed)> wrote in message
>> > news:u$(E-Mail Removed)...
>> >> How about a nice one liner
>> >> Sub deleteblankrows()
>> >> Range("a1:a6").SpecialCells(xlCellTypeBlanks).EntireRow.hidden=true
>> >> End Sub
>> >> --
>> >> Don Guillett
>> >> Microsoft MVP Excel
>> >> SalesAid Software
>> >> (E-Mail Removed)
>> >> "Harold Good" <(E-Mail Removed)> wrote in message
>> >> news:(E-Mail Removed)...
>> >>> 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 With Quote
 
Phil Hibbs
Guest
Posts: n/a
 
      26th Aug 2009
This will select from F9 downwards to the end of the contiguous set of
non-blank cells:

Private Sub Worksheet_Calculate()
Dim cells As Range
Dim cell As Range
Application.EnableEvents = False
Range("F9").Select
Selection.End(xlDown).Select
For Each cell In Range("F9:F" & Trim(Selection.Row))
If cell.Value = "" Then
cell.EntireRow.Hidden = True
End If
Next cell
Application.EnableEvents = True
End Sub

I think the solutions posted by others are superior, but I thought
this was an interesting way of avoiding processing blank rows if you
do need to do something more complex that needs a loop.

Phil Hibbs.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 Microsoft Excel Programming 2 29th Aug 2008 03:53 PM
Macro to hide rows based on criteria Santa-D Microsoft Excel Programming 5 1st Jul 2008 09:08 AM
Code help, delete rows based on column criteria =?Utf-8?B?U3RvdXQ=?= Microsoft Excel Misc 2 20th Mar 2007 01:17 PM
Code to hide Entire row based on criteria =?Utf-8?B?cmFt?= Microsoft Excel Programming 7 9th Feb 2006 09:31 PM
Code to hide rows based on cell contents Tim Microsoft Excel Programming 5 17th Dec 2003 02:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:04 PM.