+ Reply to Thread
Results 1 to 9 of 9

On error, go back a step in macro

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2004
    Location
    Cheltenham, UK
    Posts
    11

    On error, go back a step in macro

    Hi,

    I have a macro in one file which uses an input box where the user types in a subcontractor code to retrieve address and contact details from another file.

    If the user inputs the incorrect subcontractor code, an error message box pops up. Currently the user then has to select a button in the original file to restart the macro at the input box stage.

    What I want to happen is when ok is selected on the error message, to return to the input box so in my macro below where the error message says "An error occurred - you asked for a subcontractor that does not exist. Please try again." and the user selects OK, I would like it to return to the 'Enter the Subcontractor line and the "Please enter the Subcontractor code" input box reappears.

    Have searched everywhere but with my limited skills still not resolved.

    Any ideas?

    Sub Supplier()
        
    'obtain subcontractor details
        
        Dim ordSubcontractor
        Dim Subcontractor
        
        'Enter the Subcontractor
        Subcontractor = InputBox( _
            prompt:="Please enter the Subcontractor Code.")
        
        'goto subcontractor and address workbook and find details
        Application.ScreenUpdating = False
        On Error GoTo AddressError
        Windows("ORDADDSS.XLS").Activate
        ordSubcontractor = Range(Subcontractor)
         
        'write address to order
        Application.ScreenUpdating = True
        Windows("SUBCONTRACT ORDER.XLS").Activate
        Range("ordsubcontractor") = ordSubcontractor
    
    GoTo endsub
    
        'if error occurs finding address file or references
    AddressError:
        Application.ScreenUpdating = True
        Windows("SUBCONTRACT ORDER.XLS").Activate
        MsgBox "An error occured - you asked for a subcontractor " & _
                "that does not exist. Please try again."
       
    endsub:
    
    End Sub
    Last edited by girthhart; 10-09-2008 at 09:45 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Hi - your control flow is doing this by sending the code to the end of the routine ... without changing your code too much you could do something like this (my bits in red):
    Sub Supplier()
        
    'obtain subcontractor details
        
        Dim ordSubcontractor
        Dim Subcontractor
    
    InputCode:    
        'Enter the Subcontractor
        Subcontractor = InputBox( _
            prompt:="Please enter the Subcontractor Code.")
        
        'goto subcontractor and address workbook and find details
        Application.ScreenUpdating = False
        On Error GoTo AddressError
        Windows("ORDADDSS.XLS").Activate
        ordSubcontractor = Range(Subcontractor)
         
        'write address to order
        Application.ScreenUpdating = True
        Windows("SUBCONTRACT ORDER.XLS").Activate
        Range("ordsubcontractor") = ordSubcontractor
    
        GoTo endsub
    
        'if error occurs finding address file or references
    AddressError:
        Application.ScreenUpdating = True
        Windows("SUBCONTRACT ORDER.XLS").Activate
        MsgBox "An error occured - you asked for a subcontractor " & _
                "that does not exist. Please try again."
       GoTo InputCode
    
    endsub:
    
    End Sub
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You don't declare your variables as a specific type, so Excel will always interpret them as variants which can slow code down.

    What is the supplier code because you use it as Range(SubContractor)?

    You don't need to activate the other workbook.

    I would prefer to give the user an option to quit or try again.

    It's not good code practice to rely on generating an error.

    Try this code
    Option Explicit
    
    Sub Supplier()
    
    'obtain subcontractor details
        Dim ordSubcontractor As String
        Dim Subcontractor As String
    
        'Enter the Subcontractor
        Subcontractor = InputBox( _
                        prompt:="Please enter the Subcontractor Code.")
        'goto subcontractor and address workbook and find details
        On Error GoTo AddressError
        ordSubcontractor = Workbooks("ORDADDSS").Range(Subcontractor).Value
        'write address to order
        Workbooks("SUBCONTRACT ORDERS").Range("ordsubcontractor") = ordSubcontractor
        Exit Sub
        'if error occurs finding address file or references
    AddressError:
    
        Select Case MsgBox("An error occured - you asked for a subcontractor " & _
                           vbCrLf & "that does not exist. Do you want to try again.""" _
                           , vbOKCancel Or vbCritical Or vbDefaultButton1, "Incorrect subcontractor")
    
            Case vbOK
                Supplier
            Case vbCancel
                Exit Sub
        End Select
    
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    05-12-2004
    Location
    Cheltenham, UK
    Posts
    11
    Thanks for replies guys.

    RoyUk the supplier code relates to a series of named ranges in ordaddss.xls e.g. type ABCL01 in the input box & their details are dropped into the ordsupplier range in Subcontract Orders.

    Have tried your code but get the error, possibly because of this?

    Thanks

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I can't try the code without an example workbook(s)

    I think you would be better using the Find method to find the entry, not rely on Named Ranges.

  6. #6
    Registered User
    Join Date
    05-12-2004
    Location
    Cheltenham, UK
    Posts
    11
    Ok, example workbooks attached i hope!

    Subcontract Order.xls

    ordaddss.xls

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Your contractor addresses workbook is designed such that it will create extra work - adding Named Ranges, etc. Much better to use a classic Table layout so that you can use the .Find method. Also, why not keep the addresses in the PO template workbook then you could use Data Validation & VLOOKUP without VBA.

  8. #8
    Registered User
    Join Date
    05-12-2004
    Location
    Cheltenham, UK
    Posts
    11
    Ok, good advice. Thanks.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Post back when you have recreated the address book.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pause macro to enter value, continue macro
    By DamienC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2008, 09:01 AM
  2. Macro Error when File Name Changes
    By rme8223 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-17-2008, 06:18 PM
  3. vba to change macro file name
    By blackstar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-02-2007, 12:14 AM
  4. Read-only error using Form Button with Assign Macro
    By samf88 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-09-2007, 02:15 PM
  5. country finder lookup macro
    By theghost in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2007, 10:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1