Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: VBA Stop Workbook From Closing Unless Data Is Filled Complete

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    VBA Stop Workbook From Closing Unless Data Is Filled Complete

    hi can I get advice on some VBA scripting that will stop a sheet from closing under these circumstances:

    data is entered into a cell under a column call incidents - 3 other cells should also be filled out to complete
    the collection of data for the incident. some staff are not filling the 3 other cells just the main incident
    column cell and our data collection is incomplete. can we stop the sheet from closing if data exists in
    the incident column cell but not in the other 3 cells and then once data is entered the sheet can close off?

    cheers

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    It would have been better if you told us the worksheet name those cells are on and their addresses; but since you did not do that, I'll give you code and you will have to change my guesses of "Sheet1" and cells F6, H11, K6 and K7 to match your actual setup before running the code (the values you have to change are in the Const statements)...
    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Dim FilledCellCount As Long
      Const SheetContainingtheCells As String = "Sheet2"
      Const FourCellsToCheck As String = "F6,H11,K6:K7"
      FilledCellCount = WorksheetFunction.CountA(Worksheets(SheetContainingtheCells).Range(FourCellsToCheck))
      If FilledCellCount > 0 And FilledCellCount <> 4 Then
        Cancel = True
        MsgBox "Since you filled in one of the cells in the Range " & FourCellsToCheck & _
               " then you must fill in the remaining cells in that range.", vbExclamation
      End If
    End Sub
    To install this code, first press ALT+F11 to go into the VBA editor. Once there, look at the right side and locate the panel labeled "Project - VBA Project"... inside that panel are listed all your sheets and an item labeled "ThisWorkbook"... double click that item and copy/paste the above code into the code window that just opened up. That's it. If one of the cells is filled in, but not all four of them, and the user tries to close the workbook, he/she will be shown a MessageBox and then the attempt to close the workbook will be cancelled.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    thanks Rick I will give that a go .. any problems I will post that data you mentioned

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    ok the name of the entire workbook is incident spreadsheet

    incident type is entered into column G (Say G6)

    the extra data should then be entered into columns M N and O (M6 N6 and O6 would then match data in G6)

    so the script must look at data in G (again for example G6) and see data in M N and O 6 so we can be compliant with our data collection..

    hope that helps

    thanks

  5. #5
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by MATRIXOFFICE View Post
    ok the name of the entire workbook is incident spreadsheet

    incident type is entered into column G (Say G6)

    the extra data should then be entered into columns M N and O (M6 N6 and O6 would then match data in G6)

    so the script must look at data in G (again for example G6) and see data in M N and O 6 so we can be compliant with our data collection..

    hope that helps
    Okay, I misunderstood what you wanted when I read your original message... you have many rows of four data cells to check, not just four individual cells. No problem, but I need two pieces of information from you. First, I need the name of the sheet that contains the data being checked (you gave me the workbook name which I do not need). Second, what row in Column G does your data start on (that's actual data, not headers if any)?

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    thanks Rick

    sheet names are each month so there are 12 sheets containing data in this case JAN FEB MAR etc etc

    Data commences in row 5 down to row 150

  7. #7
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by MATRIXOFFICE View Post
    sheet names are each month so there are 12 sheets containing data in this case JAN FEB MAR etc etc

    Data commences in row 5 down to row 150
    Okay, I did not understand we were talking multiple sheets as well. One more question then. Given each sheet is devoted to a month, is there a need to check every sheet every time the workbook is closed? In other words, would there be any entries for the MAR (and beyond) sheet(s) yet? Also, would there be any entries for months prior to the current month? For example, tomorrow is February 1st... when this workbook is opened on February 1st, would there be any need to check the JAN sheet when it is closed later that day? Please answer both of those questions.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    no its all point in time data capture. there is no need to check past sheets only current sheets in use. there are no future entries as the data
    is based on incidents that occur. we do not know when and what they are of course ..

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    Rick any further advice on this problem?

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    Bump## anymore t houghts on this Rick?

Similar Threads

  1. Replies: 17
    Last Post: 05-22-2013, 11:58 PM
  2. Auto Complete for Data Validation
    By IJC in forum Excel Help
    Replies: 1
    Last Post: 05-15-2013, 09:30 AM
  3. Replies: 2
    Last Post: 12-04-2012, 02:05 PM
  4. VBA Code to Open Workbook and copy data
    By Howardc in forum Excel Help
    Replies: 16
    Last Post: 08-15-2012, 06:58 PM
  5. VBA code to copy data from source workbook
    By Howardc in forum Excel Help
    Replies: 1
    Last Post: 07-30-2012, 09:28 AM

Posting Permissions

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