On Error GoTo

decland

New Member
Joined
Feb 11, 2010
Messages
17
Hi

Is it possible to have the GoTo lable in a different module?
The error handling below works, but I would like it to goto a different module if the error accrues.

Code:
Sub open_Folder
 
On Error GoTo NameFolder ' Check that path is Valid
    If Dir(cnrl, vbDirectory) = vbNullString Then
                MsgBox "Folder " & cnrl & " does not exist! Check path or create folder needed and run the macro again"
        GoTo NameFolder
    End If
 
Other code
 
NameFolder:
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Why are you using On Error and checking the path if valid?

Isn't that the error you are trying to deal with?

If you do want to call code if there's an error put it in a sub and call that it there's a problem.
 
Upvote 0
Hi

I have a sub that runs all the macros in the project.

If there is an error in "Import_CNR_File" I want this maro to stop and want to skip "pm_file" & "pm_UR_file", but run the other subs.

maybe I am goining about this in the wrong way


Code:
Sub Run_Macros()
Application.ScreenUpdating = False
Application.Run "Import_CNR_File"
Application.Run "pm_file"
Application.Run "pm_UR_file"
Application.Run "Lux_Exchange_File"
Application.Run "Offshore_NAV_File"
Application.Run "UCIT_NAV_PRICING"
Application.Run "Borsen_Zeitung"
Application.Run "GTAX"
Worksheets("File locations").Select
MsgBox ("All Done")
Application.ScreenUpdating = True
End Sub

Thanks
Declan
 
Upvote 0
Declan

You don't seem to be calling the open_folder sub?:eek:
 
Upvote 0
Hi

THe project uses a number of different files, so the function open the folders, is with the different subs.

Matybe my best option is to have error handling within the "Sub Run_Macros()" that checks the files/folders used in all the subs, then error handling within the individual subs for the files/folders specific to thar sub
 
Upvote 0
Hi

I have a sub that runs all the macros in the project.

If there is an error in "Import_CNR_File" I want this maro to stop and want to skip "pm_file" & "pm_UR_file", but run the other subs.

maybe I am goining about this in the wrong way


Code:
Sub Run_Macros()
Application.ScreenUpdating = False
Application.Run "Import_CNR_File"
[COLOR=darkred]If Not ImportCNRerror Then[/COLOR]
  Application.Run "pm_file"
  Application.Run "pm_UR_file"
[COLOR=darkred]Else[/COLOR]
  [COLOR=darkred]ImportCNRerror = False[/COLOR]
[COLOR=darkred]End If[/COLOR]
Application.Run "Lux_Exchange_File"
Application.Run "Offshore_NAV_File"
Application.Run "UCIT_NAV_PRICING"
Application.Run "Borsen_Zeitung"
Application.Run "GTAX"
Worksheets("File locations").Select
MsgBox ("All Done")
Application.ScreenUpdating = True
End Sub
Maybe you can set one or more (as needed) global "flag" variables when an error occurs in your various modules and use them in the code above to filter which programs run. Let's say you had an error in "Import_CNR_File", then you could have it set global Boolean variable named, say, ImportCNRerror to True and modify your code above as shown in red to deal with it. Don't forget to clear all of the "flag" variables you add at the soonest point in your code that you can (I assumed at the point of checking in the code above, but you might need to delay it until a later time depending on what is dependent on it).
 
Upvote 0
Declan

Have you considered not calling all the code from the one main sub?

Also if this sub is only used for checking for a valid path name then you could probably use a function for that.
Code:
Function IsPathValid(strPath As String) As Boolean
 
        If Dir(strPath, vbDirectory) = vbNullString Then
                IsPathValid = True
        Else
                IsPathValid = False
        End If
End Function
Something like that could then be used in other subs whenever you want to check a path is valid.

I realise the code is probably doing more than that, eg opening a file, but perhaps even that could be converted to a function, or sub.

If you really want to use On Error etc you could put it in the calling sub.

For example:
Code:
Option Explicit
Sub test()
On Error GoTo errorthing
    Call Sub1
    Call Sub2
    Call Sub3
errorthing:
    MsgBox "There's been an error!!!!"
    End
End Sub
Sub Sub1()
Dim x
    x = 1
End Sub
Sub Sub2()
Dim x
    x = 2
    x = x / 0
End Sub
Sub Sub3()
Dim x
    x = 3
    
End Sub
The error is in Sub2 but it's caught by the On Error in the sub test.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top