View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default More Efficient code than this

It looks like you want to toggle between rows being hidden and rows being
not hidden. If so
This may be closer to what your code does.

Sub HideRows()
Application.ScreenUpdating = False
Dim rng1 as Range
Dim rng As Range
Dim c As Range
Dim cnt as Long
Dim strFirstAddress As String

Set rng = Range("E5:E204")
On Error Resume Next
set rng1 = rng.SpecialCells(xlVisible)
On Error goto 0
if rng1 is nothing then
cnt = 0
else
cnt = rng1.cnt
end if
if rng.Count < cnt then
rng.EntireRow.Hidden = False
Else

Set c = rng.Find("x", , , xlWhole)

If Not c Is Nothing Then
strFirstAddress = c.Address
Do
If c.Offset(0, 3).Value = "x" Then c.EntireRow.Hidden = True
Set c = rng.FindNext(c)
Loop Until c.Address = strFirstAddress
End If
End if

Application.ScreenUpdating = True
End Sub

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote in message
...
This will be vaguely faster but there are not really enough rows here for
there to be any great difference... I have not clocked it to see if and or
how much this improves things.

Sub HideRows()
Application.ScreenUpdating = False

Dim rng As Range
Dim c As Range
Dim strFirstAddress As String

Set rng = Range("E5:E204")

rng.EntireRow.Hidden = False

Set c = rng.Find("x", , , xlWhole)

If Not c Is Nothing Then
strFirstAddress = c.Address
Do
If c.Offset(0, 3).Value = "x" Then c.EntireRow.Hidden = True
Set c = rng.FindNext(c)
Loop Until c.Address = strFirstAddress
End If

Application.ScreenUpdating = True
End Sub

HTH

"thom hoyle" wrote:

Here is a piece of code I'm using to hide rows that have an "x" in two

of the
columns. Is there a more efficient way to write this.

thanks

Sub HideRows()
Application.ScreenUpdating = False

Dim rng As Range
Dim i As Long

Set rng = Range("E5:E204")
i = 0 'set up a variable to see if range contains hidden rows

For Each c In rng
If c.EntireRow.Hidden = True Then
c.EntireRow.Hidden = False
i = 1
End If
Next

If i = 1 Then 'if any hidden rows unhide
Application.ScreenUpdating = True
Exit Sub
End If

For Each c In rng 'if no hidden rows unhidden then hide
If c.Value = "x" And c.Offset(0, 3).Value = "x" Then
c.EntireRow.Hidden = True
End If
Next
Application.ScreenUpdating = True
End Sub