+ Reply to Thread
Results 1 to 9 of 9

Message box upon saving excel file

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2008
    Posts
    28

    Smile Message box upon saving excel file

    Is it possible to do a validation when the save button is clicked?

    Example, if cell A2 is blank, then when the save button is clicked, a message box with a message "This is a required field".

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    How about a before save event

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If IsEmpty(Sheets("Sheet1").Range("A2")) Then
        Sheets("Sheet1").Activate
        Range("A2").Select
        MsgBox "Fill in the required cell A2."
        Cancel = True
      End If
    End Sub
    HTH

    rylo

  3. #3
    Registered User
    Join Date
    06-04-2008
    Posts
    28
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If IsEmpty(Sheets("Sheet1").Range("A2")) Then
    Sheets("Sheet1").Activate
    Range("A2").Select
    MsgBox "Fill in the required cell A2."
    Cancel = True
    End If
    End Sub
    For this code, how am I suppose to modify it such a way that if B2 or any other column in B has value, A2 or any other column in A is required also?

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    How about

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      Dim ChkSH As Worksheet
      Set ChkSH = Sheets("Sheet1")
      With ChkSH
        For Each ce In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
          If Not IsEmpty(ce) And IsEmpty(ce.Offset(0, -1)) Then
            ChkSH.Activate
            ce.Offset(0, -1).Select
            MsgBox "Fill in the required cell: " & ce.Offset(0, -1).Address
            Cancel = True
            Exit For
          End If
        Next ce
      End With
    End Sub
    It will check all the cells in sheet1 column B for the used range, and if there is something in column B and nothing in the equivalent column A cell, it will put up the message and stop the save process.

    rylo

  5. #5
    Registered User
    Join Date
    06-04-2008
    Posts
    28
    That is strange i still manage to save it even when one column of A is blank and B has value

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Can you put up an example file so we can see what you have.


    rylo

  7. #7
    Registered User
    Join Date
    06-04-2008
    Posts
    28
    it will be helpful if u can solve this problem

+ Reply to Thread

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