lopiteaux
Board Regular
- Joined
- Jun 8, 2011
- Messages
- 77
Hi all - I've got a bit of code and it's all working neatly, but there is one little bit that's escaping me...
In short, the code goes through a list of agreements and updates them on a worksheet called "Credit Watch" if the risk status has been updated on the worksheet "All Agreements". That's all working beautifully.
The problem comes in towards the end, when I delete the rows that are due for update on "Credit Watch" - I've put in the following line:
In the hope that it (a) doesn't skip an agreement due to it moving up in the list (n = n - 1), which is working great, and also (b) that the check ends earlier as the list is shortened (lr = lr - 1). The first bit is working, but it seems that lr - 1 is not... I'm assuming that this is because I am specifying the lr at the beginning of the code.
Any thoughts?
l.
Code:
Sub Component_UpdateRiskStatus()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If IsEmpty(Range("A3")) Then
Exit Sub
Else
GoTo UpdateRiskStatus
End If
UpdateRiskStatus:
checksheet = ActiveSheet.Name
counter = 0
Range("A3").End(xlDown).Select
lr = ActiveCell.Row
For n = 3 To lr
checkmsg = "Now checking row #" & n & " out of " & lr & "." 'REMOVE.
checkmsg = checkmsg & vbNewLine & vbNewLine & "n: " & n
checkmsg = checkmsg & vbNewLine & vbNewLine & "lr: " & lr
checkmsg = checkmsg & vbNewLine & vbNewLine & "counter: " & counter
checkans = MsgBox(checkmsg, vbOKOnly)
x = Application.VLookup(Cells(n, "C"), Sheets("All Agreements").Columns("C:E"), 3, False)
If Not (IsError(x)) Then
If Not IsEmpty(Application.VLookup(Cells(n, "C"), Sheets("All Agreements").Columns("C:E"), 3, False)) Then
y = WorksheetFunction.Match(Cells(n, "C"), Sheets("Credit Watch").Columns("C:C"), 0)
Range("$I" & n & ":$AX" & n).Copy
Sheets("Credit Watch").Select
Range("$I" & y).Select
ActiveSheet.Paste
Range("A1").Select
Sheets(checksheet).Select
Range(n & ":" & n).Select
Selection.Delete
n = n - 1
lr = lr - 1
counter = counter + 1
Else
End If
Else
Cells(n, "C").Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
End If
Next n
MsgBox "All " & checksheet & " agreements have been checked. " & counter & " agreements have been updated on the Credit Watch list."
End Sub
In short, the code goes through a list of agreements and updates them on a worksheet called "Credit Watch" if the risk status has been updated on the worksheet "All Agreements". That's all working beautifully.
The problem comes in towards the end, when I delete the rows that are due for update on "Credit Watch" - I've put in the following line:
Code:
n = n - 1
lr = lr - 1
In the hope that it (a) doesn't skip an agreement due to it moving up in the list (n = n - 1), which is working great, and also (b) that the check ends earlier as the list is shortened (lr = lr - 1). The first bit is working, but it seems that lr - 1 is not... I'm assuming that this is because I am specifying the lr at the beginning of the code.
Any thoughts?
l.