+ Reply to Thread
Results 1 to 3 of 3

VBA code to stop the macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    VBA code to stop the macro

    Hello,

    Below code helps me to open closed Sheet and copy the particular sheet into opened sheet. But when sheet is not found, VBA still runs to next macro which built right below. I need help in closing the VBA running right below in Red line.

    Dim sImportFile As String, sFile As String
        Dim sThisBk As Workbook
        Dim vfilename As Variant
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Set sThisBk = ActiveWorkbook
        sImportFile = Application.GetOpenFilename( _
        fileFilter:="Microsoft Excel Workbooks, *.xls; *.xml; *.xlsx", Title:="Open Workbook")
        If sImportFile = "False" Then
            MsgBox "No File Selected!"
            Exit Sub
             
        Else
            vfilename = Split(sImportFile, "\")
            sFile = vfilename(UBound(vfilename))
            Application.Workbooks.Open Filename:=sImportFile
             
            Set wbBk = Workbooks(sFile)
            With wbBk
                If SheetExists("Worker") Then
                    Set wsSht = .Sheets("Worker")
                    wsSht.Copy After:=sThisBk.Sheets("Master File")
                Else
                    MsgBox "There is no sheet with name :Raw_Data in:" & vbCr & .Name  ' I need stop code here, which Stops the VBA and return to Userform1 Display.
                End If
                wbBk.Close savechanges:=False
            End With
        End If
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    Please advice
    Thanks
    Keep the Forum clean :


    1. Use [ code ] code tags [ /code ]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. Show appreciation to those who have helped you by clicking * Add Reputation below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: VBA code to stop the macro

    I use

    Exit Sub
    in my worksheet change email generator and it works fine.

  3. #3
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: VBA code to stop the macro

    Thanks!!! It worked..

+ 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. [SOLVED] VBA Code (colums to row, autofill) stop working in row 294 when new cable code appear
    By sknifseht in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-29-2012, 05:27 AM
  2. VB Code, stop at a specific row?
    By simonwilliams in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-04-2011, 04:02 PM
  3. if formula to stop now() code
    By lpratt in forum Excel General
    Replies: 7
    Last Post: 01-14-2011, 08:42 AM
  4. Code to stop a worksheet_selectionchange macro running
    By hriggs in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-17-2009, 06:23 AM
  5. [SOLVED] Stop Code
    By achidsey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-01-2006, 07:00 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