Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

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

  1. #11
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Try this

    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)  Dim FilledCellCount As Long
      Dim lngRows As Long
      Const SheetContainingtheCells As String = "Sheet2"
      Const CellsToCheck As String = "M|:O|"
      For lngRows = 5 To 150
        If Not IsEmpty(Worksheets(SheetContainingtheCells).Range("G" & lngRows)) Then
            FilledCellCount = WorksheetFunction.CountA(Worksheets(SheetContainingtheCells).Range(Replace(CellsToCheck, "|", lngRows)))
            If FilledCellCount < 3 Then
                Cancel = True
                MsgBox "Since you filled the Range 'G" & lngRows & _
                     "' then you must fill in the corresponding cells in the range - " & Replace(CellsToCheck, "|", lngRows), vbExclamation
                Exit Sub
            End If
        End If
      Next lngRows
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #12
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    The best and most plausible way to do this is to use a userform; it has been designed for this purpose (control the user's input).

  3. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    it is glitching sorry

    If Not IsEmpty(Worksheets(SheetContainingtheCells).Range( "G" & lngRows)) Then

    says that is a subscript out of range and highlights the above line

  4. #14
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    You should use double quotes when mentioning the sheet name

    So, use "" to wrap SheetContainingTheCells (you have to use the name of your worksheet here)
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  5. #15
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by MATRIXOFFICE View Post
    Bump## anymore t houghts on this Rick?
    Sorry about my absence. See if this code will do what you want...
    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Dim Mnths As Long, Rws As Long, FilledCellCount As Long, WS As Worksheet
      For Mnths = 1 To 12
        Set WS = Worksheets(UCase(Format(28 * Mnths, "mmm")))
        For Rws = 5 To 150
          If Len(WS.Cells(Rws, "G").Value) Then
            FilledCellCount = WorksheetFunction.CountA(WS.Cells(Rws, "M").Resize(, 3))
            If FilledCellCount <> 3 Then
              Cancel = True
              MsgBox "Please check Row #" & Rws & " on sheet """ & WS.Name & """" & _
                     vbLf & vbLf & "You have an incident filled in Column G for that row " & _
                     "but you are missing one or more pieces of data in Columns M thru O"
              WS.Activate
              Cells(Rws, "M").Resize(, 3).Select
              Exit Sub
            End If
          End If
        Next
      Next
    End Sub
    Last edited by Rick Rothstein; 03-17-2013 at 08:31 PM.

  6. #16
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    no probs Rick will try it out

  7. #17
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    NO good rick can I send you the actual file and you can look at it?

  8. #18
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by MATRIXOFFICE View Post
    NO good rick can I send you the actual file and you can look at it?
    Before you do that, describe "no good" for me so I have an idea what the code does or does not do correctly.

    Note: I'll be going to sleep soon, so if you respond and do not hear right back from me, you will know why.

  9. #19
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    thanks RIck - code is installed in this workbook but does not work at all - data in incident cell with no data in M N and O cells does not trigger
    the code at all

  10. #20
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Okay, I'll look at your workbook. You should be able to attach your workbook to your reply (a direct reply, not a quick reply) so anyone here can see it. If you have trouble doing that, you can send it directly to me at rickDOTnewsATverizonDOTnet (replace the upper case letters with the symbols they spell out).

    Edit note before going to sleep: I just want to make sure you realize the code I posted only executes when the workbook is closed (the wording in your last message made me wonder on that).
    Last edited by Rick Rothstein; 03-18-2013 at 02:31 PM.

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
  •