+ Reply to Thread
Results 1 to 7 of 7

Prevent user from closing worksheet if cell is blank

  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)

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    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
    Please Login or Register  to view this content.
    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. [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] 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