• Excel VBA to prevent Excel file before closing & saving

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Excel VBA to prevent Excel file before closing & saving

    Author
    Topic
    #497439

    Hi,

    I am trying to prevent the excel file to have specific cells filled prior closing & saving it. Since I am new to VBA can someone suggest me or write a VBA for me in this case ?

    Viewing 4 reply threads
    Author
    Replies
    • #1476561

      Mohan,

      Place this code in the ThisWorkbook Module. Change the cells to which ever you want cleared. You can have as many as you want. You could also apply a named range instead (ex, Scores is cells A1, B1, C1). When you attempt to save, the cells will be cleared prior.

      HTH,
      Maud

      Individual cells:

      Code:
      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
          [a1] = “”
          [b1] = “”
          [c1] = “”
      End Sub
      

      Named Range:

      Code:
      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
          [Scores].ClearContents
      End Sub
      

      From Excel, press Alt-F11 then paste the code as the image below describes.

      38490-saveclose

    • #1476562

      Put these two macros in the THISWORKBOOK module

      Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If Len(Application.Trim(Sheets(“sheet1”).Range(“a1”))) < 1 Then
      MsgBox "fill cells"
      Cancel = True
      End If
      End Sub

      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If Len(Application.Trim(Sheets("sheet1").Range("a1"))) < 1 Then
      MsgBox "fill cells"
      Cancel = True
      End If
      End Sub

    • #1476564

      I believe the BeforeClose event is not necessary as the Before save will always clear the cells before closing unless of course you do not want to save (which they will then be cleared on closing). I also think the OP wants to prevent the cells from being filled

    • #1476601

      Hey Y’all,

      Depending on the workbook requirements another option would be to just unlock all the cells, lock the ones you don’t want filled, then protect the workbook. HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1476952

      Would something like this be OK?

      Private Sub Worksheet_Change(ByVal Target As Range)
      Application.EnableEvents = False
      If Not Application.Intersect(Target, Range(“A1:A10”)) Is Nothing Then
      Target.Value = “”
      End If
      Application.EnableEvents = True
      End Sub

      Good Luck
      Alexandra

    Viewing 4 reply threads
    Reply To: Excel VBA to prevent Excel file before closing & saving

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: