+ Reply to Thread
Results 1 to 7 of 7

Prevent save and save as for specific sheet if condition within the sheet doesn't meet.

Hybrid View

Medex Prevent save and save as for... 12-22-2018, 04:03 PM
JLGWhiz Re: Prevent save and save as... 12-22-2018, 07:42 PM
Medex Re: Prevent save and save as... 12-23-2018, 03:43 AM
Alf Re: Prevent save and save as... 12-23-2018, 09:39 AM
Medex Re: Prevent save and save as... 12-23-2018, 12:18 PM
JLGWhiz Re: Prevent save and save as... 12-23-2018, 11:44 AM
JLGWhiz Re: Prevent save and save as... 12-23-2018, 12:30 PM
  1. #1
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Prevent save and save as for specific sheet if condition within the sheet doesn't meet.

    Hello Dear Members!

    I got file which you can review on attached, here I have two sheets.

    on the second sheet I need check that total sales numbers equal each other. If they are not file can not be saved.

    Actually I wrote this code. But I need this code work when I do save action on the second sheet not the first one. Vice versa when I try save any changes relate to sheet one excel doesn't allow to save.

    If there is a way to solve it please help me.

    Option Explicit
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim ST As Worksheet:        Set ST = Worksheets("Staff Targets")
    
    With ST
            If ST.Range("C" & Rows.Count).End(xlUp) <> Range("B4") Then
                Cancel = True
                MsgBox "Total Staff Target Amount is not equal to Total Monthly Target!", vbCritical, "ERROR"
            End If
    End With
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Prevent save and save as for specific sheet if condition within the sheet doesn't meet

    If you want to save a single sheet as a workbook, then use the 'Sheet.Copy' method without specifying a destination to copy to. That automatically creates a new workbook with your sheet in it. Then you can 'SaveAs' to name the file whatever you want. The methods you are using will save the entire workbook, not just the one sheet. The code for the 'SaveAs' button triggers the 'Workbook_BeforeSAve' macro causing it to make the comparison of the totals, and if they do not match, it will not do the SaveAs. Your problem is in the logic, not the code per se.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Prevent save and save as for specific sheet if condition within the sheet doesn't meet

    JLGWhiz you know what of kind things I would like to do? If I activate and working on first sheet , don't check total and save, But if I working on second sheet in this case check the total and then save it if criteria meet.
    Last edited by Medex; 12-23-2018 at 03:48 AM.

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Re: Prevent save and save as for specific sheet if condition within the sheet doesn't meet

    You have entered the "Private Sub WorkbookBeforeSave" in "ThisWorkbook" that means that this code is activated for every worksheet in your workbook and will stop you from saving if the condition specified in the macro is met in any of the sheets.

    If you cut the code and paste it to Sheet2 ("Staff Targets") the macro will only check condition on sheet2 and stop saving if condition is met only on sheet2. I.e.

    If ST.Range("C" & Rows.Count).End(xlUp) <> Range("B4") Then
    Alf
    Last edited by Alf; 12-23-2018 at 09:42 AM.

  5. #5
    Registered User
    Join Date
    06-27-2018
    Location
    Azerbaijan
    MS-Off Ver
    2013
    Posts
    29

    Re: Prevent save and save as for specific sheet if condition within the sheet doesn't meet

    I tried to write this code within "Staff Target" Sheet but, it doesn't work.
    Which part of my code is not right??

    Sub NotSave()
        
        Dim WB As Workbook:         Set WB = Workbooks("Target Sales Tmplate")
        Dim ST As Worksheet:        Set ST = Worksheets("Staff Targets")
        
        With ST
                If ST.Range("C" & Rows.Count).End(xlUp) <> Range("B4") Then
                   WB.Close SaveChanges:=False
                   MsgBox "Total Staff Target Amount is not equal to Total Monthly Target!", vbCritical, "ERROR"
                End If
        End With
        
    End Sub

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Prevent save and save as for specific sheet if condition within the sheet doesn't meet

    Try subsitituting this code for your Daily Targets SaveAs button code. It should save the Daily Targets sheet without triggering the BeforeSave macro.

    Sub SaveAsDailyTarget()
    Dim Path As String, wb As Workbook
    Dim Workbook_Name As String
    Set wb = ThisWorkbook
    Path = "C:\Users\xezer.suleymanov\Desktop\Personal Folder\ItalDizain\Report Form for Target Sales_Version 2\"
    Workbook_Name = Range("A1") & " " & Range("A2")
    Sheets("Daily Targets").Copy
    ActiveWorkbook.Saveas Filename:=Path & Workbook_Name
    wb.Activate
    End Sub
    Also, in your BeforeSave macro modify the If statement as follows.
    If ST.Range("C" & Rows.Count).End(xlUp) <> ST.Range("B4") Then
    Last edited by JLGWhiz; 12-23-2018 at 11:50 AM.

  7. #7
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Prevent save and save as for specific sheet if condition within the sheet doesn't meet

    Check post #5, using original codes.

+ 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. Replies: 12
    Last Post: 12-13-2018, 05:52 PM
  2. Need to locate specific sheet, export/save as PDF with a specific filename.
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2018, 09:24 AM
  3. How to prevent user to save doc if the rows does not meet the criteria
    By filipal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2017, 02:22 PM
  4. [SOLVED] macro to save sheet with specific file name from sheet
    By mezza89 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2013, 12:04 PM
  5. Create new directory if it doesn't exist and save sheet
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-01-2011, 01:47 PM
  6. save one sheet to specific folder but not second sheet
    By stevesunfold in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-10-2008, 04:35 AM
  7. Save specific sheet
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2007, 08:57 AM

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