+ Reply to Thread
Results 1 to 9 of 9

Ending macro early

Hybrid View

TedH Ending macro early 01-20-2009, 12:45 AM
jasoncw Hi Ted. I do not see your... 01-20-2009, 12:46 AM
TedH I have a bunch of code after... 01-20-2009, 12:56 AM
jasoncw Hi Ted. Do you receive the... 01-20-2009, 01:07 AM
shg If you need to know if a sub... 01-20-2009, 01:49 AM
TedH jasoncw: I get the "check... 01-20-2009, 10:07 AM
jasoncw Can you post the code where... 01-20-2009, 10:15 AM
TedH jasoncw: <<hand smacking... 01-20-2009, 11:42 AM
jasoncw :) Glad you figured it out. 01-20-2009, 11:45 AM
  1. #1
    Registered User
    Join Date
    11-26-2008
    Location
    New England
    MS-Off Ver
    2007
    Posts
    35

    Ending macro early

    I need to step out of a macro based on a user respone, and I don't know how to end this macro if the user needs to correct a cell entry.

    If they respond "no", I want to sub to end and select the cell where the correction needs to be made.

    Thanks
    Ted

    If Dir(filePath, vbDirectory) = vbNullString Then
            Dim FolderMsg, ResponseFolder
            FolderMsg = "This folder does not exist." & Chr(13) & Chr(13) _
               & "Do you want to create a new customer folder?"
           ResponseFolder = MsgBox(FolderMsg, vbYesNo + vbInformation)
    If ResponseFolder = vbYes Then
                 MkDir filePath
    End If
    If ResponseClose = vbNo Then
                Dim CustMsg, ResponseCustMsg As String
                CustMsg = "Check the customer name." & Chr(13) & Chr(13) _
                & "Correct if needed."
                ResponseCustMsg = MsgBox(CustMsg, vbOKOnly + vbCritical)
                Range("I5").Select
                Exit Sub  'THIS IS WHERE I NEED TO EXIT THE MACRO
    End If
    
    End If
        Rem
        Rem http://www.excelforum.com/excel-programming/663085-save-as-and-creating-directory.html
        Rem
        Rem Save the file in the proper folder
    If ActiveWorkbook.FullName = filePath & Application.PathSeparator & namePN & " PrtRev_ " _
                             & nameRev & " SO_" & nameSO & " ILRev2.xls" Then
        Dim DuplicateMsg, ResponseDuplicate
        DuplicateMsg = "This file already exists." & Chr(13) & Chr(13) _
            & "Do you want to overwrite it?"
        ResponseDuplicate = MsgBox(DuplicateMsg, vbYesNo + vbInformation)
    If ResponseDuplicate = vbYes Then
        ActiveWorkbook.Save
            
    End If
    Else
    ActiveWorkbook.SaveAs filePath & Application.PathSeparator & namePN & " PrtRev_ " _
                             & nameRev & " SO_" & nameSO & " ILRev2.xls"
    Range("e1") = "serenity now!"
    End If
        Rem
        ActiveWindow.WindowState = xlMaximized
        Calculate
        Range("F13").Select
        Rem ActiveWorkbook.Close
        
        Rem End of loop should go here
        
        Rem Dim CloseMsg, ResponseClose
        Rem PrintMsg = "Do you want to close the file?" & Chr(13)
           Rem ResponseClose = MsgBox(PrintMsg, vbYesNo + vbInformation)
        Rem If ResponseClose = vbYes Then
        Rem ActiveWorkbook.Close True
        Rem End If
    
    Else
        Dim WrongMsg As String
        WrongMsg = "This may not be an Inspectional Layout file." & Chr(13) & Chr(13) _
        & "This macro will not run on files that are not this type."
        MsgBox WrongMsg, vbOKOnly + vbCritical
       
    End If
    '
    
    End Sub
    Last edited by TedH; 01-20-2009 at 11:42 AM.

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Hi Ted. I do not see your problem. By using Exit Sub, you are exiting the macro.

  3. #3
    Registered User
    Join Date
    11-26-2008
    Location
    New England
    MS-Off Ver
    2007
    Posts
    35
    I have a bunch of code after this. If I run it as is, it continues to run the following code....

    I'll paste the code that follows in the original post.
    Ted

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Hi Ted.

    Do you receive the message box "Check the customer name. Correct if needed."? If you do, based on what you have above, the procedure should end after you click OK.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    If you need to know if a sub completes successfully, then change it to a function that returns a Boolean, and test it in the calling sub.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    11-26-2008
    Location
    New England
    MS-Off Ver
    2007
    Posts
    35
    jasoncw: I get the "check customer" msgbox; clicking yes there are no problems. On clicking "no", the end sub isn't doing it's thing; the macro continues to run because I'm getting a runtime error on the saveas function a little further down.

    shg: I'm still a noob; I usually throw a dummy expression into the worksheet to see if the code works. {i.e. "serenity now!"}

  7. #7
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Can you post the code where variable ResponseClose is assigned?

  8. #8
    Registered User
    Join Date
    11-26-2008
    Location
    New England
    MS-Off Ver
    2007
    Posts
    35
    jasoncw:

    <<hand smacking against forehead>>

    That was the problem, I copied the msgbox code from below and didn't update all the variables.

  9. #9
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Glad you figured it out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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