Samantha has attended:
Excel VBA Intro Intermediate course
On error statements
I have a For Next loop where i am searching for a variable within the loop.
I have an ON ERROR statement before the loop to say goto a stamp which says "Next" so that if the program cant find the variable, it will just do the next one.
The loop occurs three times, first with no error, second with an error and then third with an error again. And on the third error i get an "Error 91 Object variable..." error message.
I dont understand why when it gets to the second time the error occurs, it will not go to the error stamp to go Next again.
Code below:
On Error GoTo nextagent
For a = 6 To grandtotal1 - 1
'start with first agent and assign agent name
Range("A" & a).Select
agentname = ActiveCell.Value
Range("G5:J" & grandtotal2 - 1).Select
Selection.Find(What:=agentname, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
'set a variable which sets the agent found row
agentfound = ActiveCell.Row
'log the low month position
Range("E" & a).Select
'now get low position by taking the activerow and minusing 5 rows (top header rows)
positionlow = agentfound - 5
ActiveCell.Value = positionlow
'now put the increase/decrease % into column F
Range("F" & a).Select
ActiveCell.FormulaR1C1 = "=SUM(R[" & agentfound - a & "]C[4]/RC[-2])-1"
Selection.NumberFormat = "0.00%"
nextagent:
Next
Can anyone help?