+ Reply to Thread
Results 1 to 7 of 7

Prevent user from closing worksheet if cell is blank

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2013
    Location
    Fairbury NE
    MS-Off Ver
    Excel 2007
    Posts
    70

    Prevent user from closing worksheet if cell is blank

    I'm not sure how to do this and hoping someone can help. I have a worksheet that is used by multiple users. For example, in Row 2, User 1 needs to enter data into the first five columns (A-E) and User 2 needs to enter data into the next five columns (F-J). I've protected each of the areas so that each User can only access their respective area.

    What I would like to do is have User 1 complete all cells in their range (A-E) for a specific line (i.e., when something is typed into a row, that row needs to be completely filled out (A-E). Rows below the entry line (blank) do not apply. If they try to save and exit the file without doing so, I need a message prompt to state they need to complete their fields.

    My second issue is that this is a worksheet that has been used for many years and there are several cells in the rows that may be blank. Since we cannot backfill this data, the cells need to remain blank. So, the range this code would be applicable to would be the columns (A-E) and the rows from current going forward (i.e., beginning at line 1000).

    I'm a beginner in writing vba code and everything I've attempted has failed.
    Last edited by maryren; 11-15-2013 at 12:11 PM.

  2. #2
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: Prevent user from closing worksheet if cell is blank

    Okay, I think this does everything you specified, except for shifting the columns based on user. If you can get this much of it working, we can look at the multi-user part after.
    1. Open up the VBA Editor (Alt + F11)
    2. Under the VBA Project for your workbook, double-click on "ThisWorkbook"
    3. Paste all the code below (this is assuming you don't already have any code in this area)

    Option Explicit
    
    ' Property we'll use to compare number of rows at Workbook_Open, vs Workbook_Close
    Public LastRowAtOpen As Long
    
    Private Sub Workbook_Open()
        
        ' When workbook opens, capture the current last row
        Dim wb As Workbook
        Set wb = ThisWorkbook
        Dim ws As Worksheet
        Set ws = wb.Worksheets("Sheet1")
        Dim lastrow As Long
        lastrow = ws.Range("A1048576").End(xlUp).Row
        
        ' If below row 1000, reset to pass legacy rows
        If lastrow < 1000 Then
            lastrow = 1000
        End If
        
        ' Set property against which to check when closing workbook
        LastRowAtOpen = lastrow
        
        
        Set ws = Nothing
        Set wb = Nothing
        
        
    End Sub
    
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
        If LastRowAtOpen < 1000 Then
            LastRowAtOpen = 1000
        End If
        
        Dim wb As Workbook
        Set wb = ThisWorkbook
        Dim ws As Worksheet
        Set ws = wb.Worksheets("Sheet1")
    
        ' Check last row
        Dim lastrow As Long
        lastrow = ws.Range("A1048576").End(xlUp).Row
    
        ' If below row 1000, reset to pass legacy rows
        If lastrow < 1000 Then
            lastrow = 1000
        End If
    
        ' Compare current lastrow to LastRowAtOpen
        If lastrow > LastRowAtOpen Then
    
            ' At least one row was added
            ' Check each new row for empty cells
    
            Dim problemRows As String
            problemRows = vbNullString
    
            Dim rng As Range
            Set rng = ws.Range("A" & LastRowAtOpen + 1 & ":E" & lastrow)
    
            Dim rw As Long
            Dim cl As Variant
    
            For rw = 1 To rng.Rows.Count
                For Each cl In rng.Rows(rw).Cells
                    If cl.Value = vbNullString Then
                        If problemRows = vbNullString Then
                            problemRows = ws.Range(cl.Address).Row
                        Else
                            problemRows = problemRows & ", " & ws.Range(cl.Address).Row
                        End If
                        Exit For
                    End If
                Next cl
            Next rw
        End If ' lastrow > LastRowAtOpen
    
    
        If problemRows = vbNullString Then
            ' No problems, okay to close
            Cancel = False
            SaveAsUI = True ' Allows launch of "SaveAs" dialog
        Else
            ' Problems
            ' Alert user and block save
            Cancel = True
            MsgBox ("Please complete data entry in row(s): " & problemRows)
            
        End If ' problemRows = vbNullString
    
    
        Set rng = Nothing
        Set ws = Nothing
        Set wb = Nothing
    
    End Sub
    Note, you will not be able to save the workbook if there is anything already below line 1000. If that is a problem, change all the places in the code where I specify "1000" to be your actual last row of data.

  3. #3
    Registered User
    Join Date
    06-12-2013
    Location
    Fairbury NE
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Prevent user from closing worksheet if cell is blank

    I could not get the code from gyclone to work and tried another code (below) and could not get that to work either. Basically, I need my range in the worksheet to begin at row 5494 and continue as new rows are created. I'd like the range to be set that if something is entered in column D of the row, Column A and C need to be completed (Column B is hidden, no entry required). The first time I try the code, it works fine and gives me the message prompt or saves the file, but when I reopen it and try again I get an error. Hoping someone can help. Below is the code I was trying to use. The "de-bug" option highlights "rMyRng.SpecialCells(xlCellTypeBlanks).Select" but I'm not sure that is the only issue.


    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim nRws As Long, nFilCnt As Long, rMyRng As Range

    With Sheets("DOMESTIC")
    nRws = .Cells(Rows.Count, "D").End(xlUp).Row
    Set rMyRng = Range("a5494:d" & nRws)
    nFilCnt = WorksheetFunction.CountA(rMyRng)
    If (nRws - 1) * 3 <> nFilCnt Then
    rMyRng.SpecialCells(xlCellTypeBlanks).Select
    MsgBox "Fill The Selected Blank Cells to Exit This File", vbCritical, "Action Required"
    Cancel = True
    End If
    End With
    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: Prevent user from closing worksheet if cell is blank

    I could not get the code from gyclone to work
    What do you mean; what happens when you try?

    If you mean that it runs, but didn't do what you want, that's because what you asked for the first time is not actually what you wanted, based on your last post. If you get an error, what is the error?

    As for your new code, look at this (same code with comments added telling you what is actually happening). See if that helps you see the problems.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim nRws As Long, nFilCnt As Long, rMyRng As Range
    
        With Sheets("DOMESTIC")
            
            ' set nRws to last row of data in column D
            nRws = .Cells(Rows.Count, "D").End(xlUp).Row
            
            Set rMyRng = Range("a5494:d" & nRws)
            
            ' Count number of cells that are NOT empty
            nFilCnt = WorksheetFunction.CountA(rMyRng)
            
           ' if total number of rows in worksheet, minus one, times 3, does not equal the total numer of non-blank cells in range
            ' it never will, it's impossible
            ' number of rows in worksheet is at least 5494 rows more than in range
            ' nRws needs to be the number of rows in range, not in worksheet, for this to work
            If (nRws - 1) * 3 <> nFilCnt Then
                ' Will throw error if no blank cells in range
                ' Correcting the calculation for nRws will prevent getting here if no blank cells
                ' But, right now, the code will always get here, whether there are blanks or not
                rMyRng.SpecialCells(xlCellTypeBlanks).Select
                sgBox "Fill The Selected Blank Cells to Exit This File", vbCritical, "Action Required"
                Cancel = True
            End If
        End With
        
        
    End Sub
    Last edited by gyclone; 11-13-2013 at 07:27 PM. Reason: reworded code comment for clarity/correction (see highlight)

  5. #5
    Registered User
    Join Date
    06-12-2013
    Location
    Fairbury NE
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Prevent user from closing worksheet if cell is blank

    Hi Glycone,

    I am a beginner in VBA so am not sure what I need to write.

    My worksheet is currently populated with information in rows 1-5494. Many of these rows (97%) do not have any text entered into the date column (Column C).

    Since the data spans over many years, it is not possible to go back and populate those cells.

    However, going forward, I would like to enforce the discipline of entering the date (Column C) before the worksheet is closed, provided that the cell in Column D is populated with a part number once a new line/row is typed into beginning with row 5495.

    Hoping you can provide insight.

    Thanks,

  6. #6
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Prevent user from closing worksheet if cell is blank

    Maybe
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim lColumnC As Long
    Dim lColumnD As Long
    
    lColumnC = Application.WorksheetFunction.CountA(Range("C5495:C" & Range("C" & Rows.Count).End(xlUp).Row))
    lColumnD = Application.WorksheetFunction.CountA(Range("D5495:D" & Range("D" & Rows.Count).End(xlUp).Row))
    
    If lColumnC <> lColumnD Then
        MsgBox "Not all dates entered for part numbers.  Please fix before closing."
        Cancel = True
    End If
    
    End Sub
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  7. #7
    Registered User
    Join Date
    06-12-2013
    Location
    Fairbury NE
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Prevent user from closing worksheet if cell is blank

    Solus,
    Thanks for this, it works perfectly.

+ 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. [SOLVED] Prevent the users from closing the worksheet
    By jy7788 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2013, 05:44 AM
  2. Prevent user from typing on worksheet?
    By okriskao in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-12-2009, 04:09 PM
  3. Prevent a user from closing a userform
    By BigBas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2007, 03:03 PM
  4. prevent blank cell in excel worksheet
    By jatman in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-29-2006, 10:08 AM
  5. prevent a user leaving a blank cell in excel2003
    By Ian Varty in forum Excel General
    Replies: 1
    Last Post: 04-15-2005, 09:06 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