Results 1 to 7 of 7

Add Instruction Code For NO or CANCEL within ActiveWorkbook.SaveAs

Threaded View

  1. #1
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Add Instruction Code For NO or CANCEL within ActiveWorkbook.SaveAs

    Hi all,

    When my code runs, it tries to create individual booklets from each sheet.
    If a booklet exists with the same name of the booklet its trying to create, a
    message window pops up, giving the user the option to select from (SAVE,NO,CANCEL).

    If the user selects SAVE, it overwrites the existing file.
    If the user slects NO, the code errors out.
    If the user selects CANCEL, the code errors out.

    I need some code that will be able to handle the NO and CANCEL operations.

    I'm struggling on adding this to my code.

    I'd like for there to be a SAVE_AS if the user selects NO.
    I'd like for there to be a Exit Sub, if the user selects CANCEL

    Sub SaveUpdatedBOM()
    Dim Cancel As Boolean
      'Split worksheets in current workbook into
      ' many separate workbooks  D.McRitchie, 2004-06-12
      'Close each module  AND the VBE before running to save time
      ' provides a means of seeing how big sheets really are
      'Hyperlinks and formulas pointing to other worksheets within
      ' the original workbook will usually be unuseable in the new workbooks.
        Dim CurWkbook As Workbook
        Dim wkSheet As Worksheet
        Dim newWkbook As Workbook
        Dim wkSheetName As String
        Dim shtcnt(3) As Long
        Dim xpathname As String, dtimestamp As String
        dtimestamp = Format(Now, "yyyymmdd_hhmmss")
        xpathname = Sheets("BOM").Range("C18").Value & "\"
    
        Set CurWkbook = Application.ActiveWorkbook
        
        shtcnt(2) = ActiveWorkbook.Sheets.Count
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
          If Cancel = True Then
            Exit Sub
             Else
        For Each wkSheet In CurWkbook.Worksheets
          shtcnt(1) = shtcnt(1) + 1
      
          Application.StatusBar = shtcnt(1) & "/" & shtcnt(2) & _
              "  " & wkSheet.Name
          wkSheetName = Trim(wkSheet.Name)
          If wkSheetName = Left(Application.ActiveWorkbook.Name, _
             Len(Application.ActiveWorkbook.Name) - 4) Then _
             wkSheetName = wkSheetName & "_D" & dtimestamp
          Workbooks.Add
          
          
             ActiveWorkbook.SaveAs _
             Filename:=xpathname & wkSheetName & ".xls", _
             FileFormat:=xlNormal, Password:="", _
             WriteResPassword:="", CreateBackup:=False, _
             ReadOnlyRecommended:=False
             
          
            
             
          Set newWkbook = ActiveWorkbook
          
          Application.DisplayAlerts = False
          newWkbook.Worksheets("sheet1").Delete
          On Error Resume Next
          newWkbook.Worksheets(wkSheet.Name).Delete
          On Error GoTo 0
          Application.DisplayAlerts = True
          
          CurWkbook.Worksheets(wkSheet.Name).Copy Before:=newWkbook.Sheets(1)
          'no duplicate sheet1 because they begin with "a"
          ActiveWorkbook.Save
          ActiveWorkbook.Close
        Next wkSheet
        Application.StatusBar = False      'return control to Excel
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
      Kill (xpathname & "MASTER.xls")
       Kill (xpathname & "BOM_INSERT.xls")
       
    
     RemoveAnyMacros2
     BringToFrontAndReformatBOM
     
      Range("A1").Select
      Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
    
    'ChangeFilename
      MsgBox "Done"
      End If
        
    End Sub

    Any help with this is greatly appreciated.


    BDB
    Last edited by bdb1974; 02-26-2010 at 05:19 PM.

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