View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Repeat a marco with number of times or loop until End

Hi again elton,

Re-reading your question; perhaps what you mean is that you want to return a
flag to the loop if an error has occurred in the called sub and therefore
want to exit the loop. If so then the following.

At the top of the VBA editor before any subs insert the following declaration.

Public errorFlag As Boolean

Then your loop code like the following.

Sub RunInLoop()
Dim i As Long
errorFlag = False 'Initialize to false

For i = 1 To 100
If errorFlag = True Then
Exit For
End If
Application.Run "SkyDaysHour_X31_H2_Hour"
Next i

End Sub


and include the following code in your called sub

Sub SkyDaysHour_X31_H2_Hour()

'The following line goes immediately prior
'to the line/s that likely to produce error
On Error GoTo SubError

'The code likely to produce error here

'The following immediately after the
'code likely to produce error
On Error GoTo 0 'Resumes errror trapping

'The following 3 lines of code are the
'last lines of code before the End Sub

Exit Sub
SubError:
errorFlag = True

End Sub


--
Regards,

OssieMac


"Elton Law" wrote:

Dear Expert,
I know this may be easy for some of the experts, but I still how you can
help ...


Say following scripts ...
I would like to run this scripts for 100 times ...
Can you advise how to write?

Application.Run "SkyDaysHour_X31_H2_Hour"


Can you also tell how to write loop until it reach error and jump to next
step ..?

Thanks