How to disable closing workbook before filling in specific cells (if empty)

K0st4din

Active Member
Joined
Feb 8, 2012
Messages
488
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone,
please help you to complete this macro:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sht As Worksheet

Set sht = Application.ThisWorkbook.Worksheets("ime slyjitel")

Application.ScreenUpdating = False

    If sht.Range("D3") = "" Then
        MsgBox "text, text, text, text, text, text, text, "
        End
    ElseIf sht.Range("C2") = "" Then
        MsgBox "text, text, text, text, text, text, text, "
        End
    ElseIf sht.Range("D4") = "" Then
        MsgBox "text, text, text, text, text, "
        End
   ElseIf sht.Range("G4") = "" Then
        MsgBox "text, text, text, text, text, "
        End
   ElseIf sht.Range("B4") = "" Then
        MsgBox "text, text, text, text, "
        End
       
    Else
    End If
    
Application.ScreenUpdating = True
End Sub

Everything in it works, but I don't know how to add something that forbids closing the workbook before the mentioned cells are filled in if they are empty.
Thank you in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi,

try following

In standard module

VBA Code:
Function AllComplete() As Boolean

    Dim rng As Range
    
     With Application
        Set rng = .ThisWorkbook.Worksheets("ime slyjitel").Range("B4,C2,D3:D4,G4")
        AllComplete = .CountA(rng) = rng.Cells.Count
    End With
    rng.Interior.ColorIndex = xlNone
    If Not AllComplete Then
        MsgBox "Complete All Cells", 48, "Entry Required"
        rng.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 3
    End If
    
End Function

In Thisworkbook Code Page

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = Not AllComplete
End Sub

Dave
 
Upvote 0
Hello dmt32
the macro is very clever,
but in my, text that is displayed, it actually tells the user exactly what he forgot to write.
If possible, let's update it on my macro, I don't need to color the cells because the worksheets are locked.
And I don't want to insert extra lines to unlock and lock while the macro is running.
Thanks in advance :)
 
Upvote 0
hi,
try this update

Rich (BB code):
Function AllComplete() As Boolean
    Dim i As Integer
    Dim msg As String
    Dim rng As Range, cell As Range

    Set rng = Application.ThisWorkbook.Worksheets("ime slyjitel").Range("B4,C2,D3:D4,G4")
    
    For Each cell In rng.Cells
    i = i + 1
    msg = Choose(i, "msg1", "msg2", "msg3", "msg4", "msg5")
    AllComplete = Len(cell.Value) > 0
    If Not AllComplete Then
        MsgBox "Complete " & msg, 48, "Entry Required"
        rng.Parent.Activate
        cell.Select
        Exit Function
    End If
    Next cell
End Function

Change msg prompts for each cell shown in BOLD as required

Dave
 
Upvote 0
It's great now.
Thank you very much.
Be alive and healthy and I wish you a pleasant weekend!
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top