View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Dana DeLouis[_3_] Dana DeLouis[_3_] is offline
external usenet poster
 
Posts: 690
Default Repeat a marco with number of times or loop until End


Hi. Don't know what you have set up, but would any ideas here help?

Option Explicit
Private ErrorOccuredQ As Boolean

Sub MyLoop()
ErrorOccuredQ = False
Do While Not ErrorOccuredQ
Run "SkyDaysHour_X31_H2_Hour"
If ErrorOccuredQ Then Exit Do
Loop
End Sub


Sub SkyDaysHour_X31_H2_Hour()
'Do Stuff

'If some type of error occurs
ErrorOccuredQ = True
'end if
End Sub

= = = = = = =
HTH
Dana DeLouis


On 3/28/2010 11:22 AM, Elton Law wrote:
Hi Expert,
Thanks for your reply.
I should have typed more clearly.

If I want to repeat this action in a marco.
I would copy and paste ...

Application.Run "SkyDaysHour_X31_H2_Hour"
Application.Run "SkyDaysHour_X31_H2_Hour"
Application.Run "SkyDaysHour_X31_H2_Hour"
Application.Run "SkyDaysHour_X31_H2_Hour"
Application.Run "SkyDaysHour_X31_H2_Hour"
Application.Run "SkyDaysHour_X31_H2_Hour"
Application.Run "SkyDaysHour_X31_H2_Hour"
..............
.............
............ on and on until 100 times ....

Sound like it is very stipud.
I would like to know whether there is script ....
Say define at the begining and run this action for 100 times rather than
copy/paste this sentence for 100 times.
Hope you know what I mean.

Another one is ....

I even don't know when this action will be ended .... (sometimes it is less
than 100 times)
So just let it re-run and re-run .... until it cannot run due to error ...
Then jump to next step.... (maybe less than 100 times)

Hope you can help...
Regards,
Elton





"OssieMac" wrote:

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





--
= = = = = = =
HTH :)
Dana DeLouis