What's wrong with this code?
The first thing I notice is 3 nested loops: i=5 to 10, LookupRow=3 to ? (row
number), and rwIndex = 3 to 505. For each value of i, Lookup row has to run
for each row (let's say you have 500 rows??? - that would give 2500 passes
through your code), and then you do rwIndex another 500 times (approx) for
1,250,000 passes through the inside loop. That is a lot of work and would
take a long time to execute - it would appear "hung" until done, possibly
several minutes. You could check its progress periodically by Ctrl-Break and
use the immediate pane to print i, LookupRow, and rwIndex at that point, then
restart the code and do it again later.
If you could explain what you are trying to do there may be a way to avoid
some of the looping... the best way to speed up your code.
--
- K Dales
"PhilipsBernard" wrote:
Hi!
I'm kinda new to VBA programming, so maybe you can help me...
When I run the following macro, the program seems to crash (doesn't
respond). Is there a fault in the code or is there another explanation? All
advice welcome!
Sub FindActuals()
Actualmonth = Worksheets("COOMonthWCYTD").Range("J5")
'On Error GoTo codeBreak
'For i = 5 To 10
For LookupRow = 3 To Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set LookupAcc = Sheets(10).Cells(LookupRow, 2)
For rwIndex = 3 To 505
With Sheets(Actualmonth).Cells(rwIndex, 2)
Sheets(Actualmonth).Select
Sheets(Actualmonth).Cells(rwIndex, 2).Activate
If LookupAcc.Value = ActiveCell.Value Then
OffsetCount = -1
If Not ActiveCell.Offset(OffsetCount, -1) =
ActiveCell.Offset _(0, -1) - 1 Then
If LookupAcc.Offset(0, -1).Value =
ActiveCell.Offset _(OffsetCount, 0).Value Then
LookupAcc.Offset(0, 5).Value =
ActiveCell.Offset _(OffsetCount, 2).Value
Else: OffsetCount = OffsetCount - 1
End If
End If
End If
End With
Next rwIndex
Next LookupRow
'Next i
'codeBreak: Exit Sub
End Sub
|