+ Reply to Thread
Results 1 to 21 of 21

VBA to prevent saving of excel if cell is blank

  1. #1
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    VBA to prevent saving of excel if cell is blank

    Dear Team

    please help me in VBA code to restrict saving of Excelfile if there is any value in column A and no data in corresponding cell in column B,C,E,F,G,H,I,J,K,N,O,P,V,AA is left blank

    Also i want to restric copy pasting of data in the excel sheet(or on perticual cells), so that evry team member should enter the data manually

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    If your columns are perfectly balanced (no extra entries in column A, or any of the other columns) then you can use this in the code module of the Thisworkbook object in your project:

    Please Login or Register  to view this content.
    To prevent pasting on a specific sheet, right-click the sheet tab, select "View Code" and paste this code into the window that appears

    Please Login or Register  to view this content.
    To prevent specific ranges from being pasted over, use

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hye Bernie
    thanks for helping but the code is not working

    The scenario i am speaking, would have data in A column for sure but the coresponding cells in other columns like B,C,E,F,G,H,I,J,K,N,O,P,V,AA is occasionaly not filled by users

    I required to see if there is any data in A1 to A2000 (example the rows could vary)the corresponding cells of columns B,C,E,F,G,H,I,J,K,N,O,P,V,AA (1 to 2000) to also be filled in accrodance or sheeet should not be saved

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Try it this way:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hey Bernie

    Wow that was quick, i can check if the code is working or not as i am getting the error

    Run Time error "1004" No Cells were found

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Is A filled with values, or formulas? You should be able to just change

    For Each rngC In Intersect(ActiveSheet.UsedRange, Range("A:A")).SpecialCells(xlCellTypeConstants)

    to

    For Each rngC In Intersect(ActiveSheet.UsedRange, Range("A:A"))

  7. #7
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hey bernie

    All the cells in column A would have values no formula

  8. #8
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hey Bernie your code works like a charm

    but i have one other issue now in the excel i have 2 VB codes which i need to put on eis yours and the other code is for checking if there is a validation in the sheet if there is and any user tries to copy paste on the cell which has validation excel would not allow to do so

    the code is
    Please Login or Register  to view this content.
    attached is the file

    the problem is that the sheet goes in loop and gives run time error 28 out of stack space

    Also i can i stop user to copy paste complete row in the sheet from other source
    Attached Files Attached Files
    Last edited by anuj_sethi1043; 08-13-2014 at 07:20 AM.

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Please Login or Register  to view this content.
    Though this would work to prevent the pasting...

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hey bernie thanks for the code it works bang on

    but user can still copy and paste entire row from another workbook and paste on this workbook bypassing the restrictions we had put

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Doesn't your Worksheet_Change event stop the entire row pasting?

  12. #12
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    no it does not

  13. #13
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Try using this change event for the sheet with the data validation.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    hey bernie..if i copy and paste an entire row from different worksheet it allows to do that

  15. #15
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    When I tested it, the change in the cells in ValidationRange triggered the undo...

    When you say Worksheet, do you mean another workBOOK? We could use the workbook activation event to set the cutcopymode to false.....

  16. #16
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    hey yes i meant another workbook..

  17. #17
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Then put this into the Thisworkbook object's codemodule:

    Private Sub Workbook_Activate()
    Application.CutCopyMode = False
    End Sub

  18. #18
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Talking Re: VBA to prevent saving of excel if cell is blank

    Hey bernie

    tried putting the code in thisworkbook but it did notwork
    can you place the same in sample workbook or in the one i attached would really appreciate that

    Private Sub Workbook_Activate()
    Application.CutCopyMode = False
    End Sub

  19. #19
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Here you go....

    Business MIS Format.xlsm

    This is the code now, which should disable all pasting


    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    hey this code disabled the copy paste option itself

    can we disable copy paste only on the range defined or specific column require..
    is it possible?

  21. #21
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,953

    Re: VBA to prevent saving of excel if cell is blank

    Try being more specific.


    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hey bernie..sorry was out of town so could not reply, i would try to explain in detail

    the VB code works like charm and stops user to copy and paste in the excel
    but if user tries to copy and paste..a complete row from other excel workbook, it is easy possible which i wanted to restrict

  23. #23
    Registered User
    Join Date
    04-24-2009
    Location
    delhi,india
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA to prevent saving of excel if cell is blank

    Hey bernie..sorry was out of town so could not reply, i would try to explain in detail

    the VB code works like charm and stops user to copy and paste in the excel
    but if user tries to copy and paste..a complete row from other excel workbook, it is easy possible which i wanted to restrict

+ 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. Prevent saving of excel if certain condition not fulfil
    By caabdul in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-08-2014, 03:41 AM
  2. [SOLVED] Looping through cell ranges in multiple worksheets and prevent saving if cell value empty
    By Spritz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-22-2013, 11:24 AM
  3. prevent cell values modification after saving the worksheet.
    By bittu2010 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-09-2012, 03:12 AM
  4. [SOLVED] prevent blank cell in excel worksheet
    By jatman in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-29-2006, 10:08 AM
  5. [SOLVED] How to prevent saving over Excel and Word Files
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-27-2005, 11:05 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