Consulting

Results 1 to 3 of 3

Thread: If cell is Empty!

  1. #1
    VBAX Newbie
    Joined
    Oct 2019
    Posts
    1
    Location

    If cell is Empty!

    Hi all,

    I am trying to check for all sheets if specific call E10 is empty and it prevents user from saving.
    Currently it is only working on the sheet that i am on.
    It does not work on the others, i want to make it that it checks all check for a specific cell before saving.
    This is currently what i have.

    Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Cells(10, 5).Value = "" Then
    Cancel = True
    MsgBox "Save cancelled"
    End If
    End Sub

    Thanks for helping!!

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Change Sheet Names
    Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Sheet1.Cells(10, 5).Value = "" Then
        Cancel = True
        MsgBox "Save cancelled"
    ElseIf Sheet2.Cells(10, 5).Value = "" Then
        Cancel = True
        MsgBox "Save cancelled"
    ElseIf Sheet3.Cells(10, 5).Value = "" Then
        Cancel = True
        MsgBox "Save cancelled"
    End If
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Option Explicit
    
    
    Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim ws As Worksheet
        
        For Each ws In Worksheets
            If Len(ws.Range("E10").Value) = 0 Then
                Cancel = True
                MsgBox "Save cancelled"
                Exit Sub
            End If
        Next
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •