View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Philip Philip is offline
external usenet poster
 
Posts: 156
Default VBA (Optimization) Help to replace inefficient Do .. Loops ...

Hi,

I have several Do.. Loops in different Case statements in a sub like this:
CODE <<<<

[P3].Select

' Autofilter didn't work properly, so deleting ALL non-SETL
rows...
' FOR THIS REASON, it is important to run the SETTLED LAST !!!
Do Until ActiveCell.Text = ""

If ActiveCell.Text < "SETL" Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Select
End If

Loop

'hide the lines with the excluded broker numbers
[S3].Select

Do Until ActiveCell.Text = ""
For iExCount = 0 To UBound(iExcludedBrks)
If ActiveCell.Text = iExcludedBrks(iExCount) Then
ActiveCell.EntireRow.Delete
ActiveCell.Offset(-1, 0).Select
Exit For
End If
Next
ActiveCell.Offset(1, 0).Select
Loop

Columns("R:R").Hidden = False ' LAST UPDATE COLUMN MUST NOT BE HIDDEN !!!
[R3].Select

' first loop, delete ALL rows with LAST UPDATE before LAST RUN, as these are
ALL SETTLED !
Do Until ActiveCell.Text = ""
NextDate:
If ActiveCell.Text = "" Then Exit Do
iTestDate = CDate(Format(VBA.Left(VBA.Trim(ActiveCell.Text),
4) & "/" & VBA.Mid(VBA.Trim(ActiveCell), 5, 2) & "/" &
VBA.Mid(VBA.Trim(ActiveCell), 7, 2), "Short Date"))

If iTestDate < CDate(sLastUpdateDate) Then
' this LAST UPDATE was before LAST RUN DATE , so now time check required,
delete
ActiveCell.EntireRow.Delete shift:=xlUp

GoTo NextDate
End If
ActiveCell.Offset(1, 0).Select

Loop

[R3].Select
' now we have deleted ALL activity that settled BEFORE the LAST RUN, so with
the remainder,
' check the time part of the LAST UPDATE FIELD AGAINST TIME LAST RUN !!!
' Modified PWL 2002 02 26
' To force a test of BOTH conditions ...
' Both the DATE of LAST UPDATE AND TIME LAST UPDATE in MCH MUST
be before LAST RUN date/time
' for a record to be deleted...
Do Until ActiveCell.Text = ""
NextTime:
If ActiveCell.Text = "" And ActiveCell.Offset(1, 0).Text =
"" Then Exit Do

' iTestTime = TimeValue(VBA.Left(VBA.Right(VBA.Trim(ActiveCell.T ext), 6),
2) & ":" & VBA.Mid(VBA.Right(VBA.Trim(ActiveCell.Text), 6), 3, 2) & ":" &
VBA.Right(VBA.Right(VBA.Trim(ActiveCell.Text), 2), 6))

' it tests the values of the 'seconds' part of the DATE TIME LAST UPDATE
field from
' MCH, and if that is = 60 then it is replaced by 59.
' CHANGED PWL, Aug 2003 for MCH Issue in CTDA/BTDA...
If VBA.Right(VBA.Right(VBA.Trim(ActiveCell.Text), 2), 6) = 60 Then
iTestTime =
VBA.TimeValue(VBA.Left(VBA.Right(VBA.Trim(ActiveCe ll.Text), 6), 2) & ":" &
VBA.Mid(VBA.Right(VBA.Trim(ActiveCell.Text), 6), 3, 2) & ":59")
Else
iTestTime =
VBA.TimeValue(VBA.Left(VBA.Right(VBA.Trim(ActiveCe ll.Text), 6), 2) & ":" &
VBA.Mid(VBA.Right(VBA.Trim(ActiveCell.Text), 6), 3, 2) & ":" &
VBA.Right(VBA.Right(VBA.Trim(ActiveCell.Text), 2), 6))
End If
iTestDate = CDate(Format(VBA.Left(VBA.Trim(ActiveCell.Text), 4) & "/" &
VBA.Mid(VBA.Trim(ActiveCell), 5, 2) & "/" & VBA.Mid(VBA.Trim(ActiveCell), 7,
2), "Short Date"))

' check the last UPDATE TIME OF THE RECORD against LAST RUN TIME
' BY THE SECOND !!!
If (iTestTime < TimeValue(sLastUpdateTime)) And (iTestDate =
CDate(sLastUpdateDate)) Then
' e.g. IF time of LAST RUN was 09:01:00 and Time LAST UPDATE was 09:00:59
' THEN THAT ROW WILL BE DELETED !!!
' Time of last update was BEFORE last run, so delete
this row!
ActiveCell.EntireRow.Delete shift:=xlUp
GoTo NextTime
End If
ActiveCell.Offset(1, 0).Select
Loop

<<< END CODE

I am sure there must be a better way... for example, I have a list of
brokers to exclude from the report in an array, and for each row I check all
the brokers...

I'd be gratefule for any help to replace this with more efficeint code in
case there are many thousands of trades being checked...

thanks

Philip