View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_1493_] Rick Rothstein \(MVP - VB\)[_1493_] is offline
external usenet poster
 
Posts: 1
Default Code runs to slow

Ignore what I said about my timing... I misunderstood your posting and
thought your code measured the time-difference in a single run... I didn't
realize it was a two-step process requiring the bFastTest be flipped
manually for the two cases... the time I reported was the time it took to
run the "chunk" method. I had to break into the "non-chunk" method as it was
taking too long. Again, thanks for pointing this problem out. By the way,
for those out there following this thread, the reason all this is important
is the Union technique has application to deleting rows of data (a
relatively common question in these groups) and the apparently recommended
method is to iterate the data rows, from the last data row to the first,
deleting rows as you go... this Union technique (still requiring the
backward iterations if using Peter T's "chunk" method) promises to be an
enormously quicker approach, so it is important to fine-tune it as much as
is possible.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
I got a difference of 1.18359375 seconds when I ran your code; but I have a
pretty fast computer here, so that time differential may not be
representative. In any case, point taken... limit the unionizing to groups
of 100 or so... even doing that should still produce an enormous
improvement over doing it in the straight forward, one-at-a-time iteration
approach. Thanks for pointing that out.

Rick


"Peter T" <peter_t@discussions wrote in message
...
One thing to watch out for with Union is that it becomes exponentially
slower if/as the number of discontiguous areas in the unioned range
increase. Typically it's faster to process batches of 50-400 areas at a
time. The exact qty of areas depends on what the process is but a good
rule
of thumb is limit to about 80-100.

The following should demonstrate the difference, trying to union 20k
areas
is not viable.

Sub UnionDemo()
Dim bFastTest As Boolean
Dim bGotRng As Boolean
Dim i As Long
Dim arr() As Long
Dim rHide As Range

Rows.Hidden = False
Columns(1).Clear

Set rng = Range("A1:A40000")

ReDim arr(1 To rng.Rows.Count, 1 To 1)
For i = 1 To UBound(arr) Step 2
arr(i, 1) = 1
Next

rng.Value = arr

bFastTest = True
' bFastTest = False ' reduce rng size above to say 4k or less for
testing

t = Timer ' sensitive timer not required !

If bFastTest Then

For Each cell In rng
If cell.Value = 0 Then
If bGotRng Then
Set rHide = Union(rHide, cell)
If rHide.Areas.Count 100 Then
rHide.EntireRow.Hidden = True
Set rHide = Nothing
bGotRng = False
End If
Else
Set rHide = cell
bGotRng = True
End If
End If
Next

If Not rHide Is Nothing Then ' or If bGotRng ...
rHide.EntireRow.Hidden = True
End If

Else ' do ctrl-break if it's taking to long
Set rHide = Nothing
For Each cell In rng
If cell.Value = 0 Then
If rHide Is Nothing Then
Set rHide = cell
Else
Set rHide = Union(rHide, cell)
End If
End If
Next

If Not rHide Is Nothing Then
rHide.EntireRow.Hidden = True
End If

End If
Debug.Print Timer - t

End Sub

Regards,
Peter T


"Rick Rothstein (MVP - VB)" wrote
in
message ...
I know you have a different solution now, but your comment about the
speed
of the code I posted for you earlier got me to thinking. Probably the

speed
problem is due to the continual hiding of the rows one-by-one. I'm

thinking
the code below should be more efficient. If you have the chance, I would

be
interested in how fast the code below is compared to the code I gave you
earlier.

Sub HideRowIfZeroInJ()
Dim R As Range
Dim RowsToHide As Range
Dim LastRow As Long
Application.ScreenUpdating = False
With Worksheets("Sheet3")
LastRow = .Cells(Rows.Count, "J").End(xlUp).Row
For Each R In .Range("J3:J" & CStr(LastRow))
If R.Value = 0 And R.Value < "" Then
If RowsToHide Is Nothing Then
Set RowsToHide = R
Else
Set RowsToHide = Union(R, RowsToHide)
End If
End If
Next
If Not RowsToHide Is Nothing Then RowsToHide.EntireRow.Hidden = True
End With
Application.ScreenUpdating = True
End Sub

Rick

<snip