Hi,
I found the following snippet of code (below) to check for required fields that are empty in an excel 2007 document. What I want to do is point out to the user that the fields are empty and are required to be filled in. I haven't tried it yet but reviews of the code seem to point that it will work for what I want to do. What I don't know how to do though is to activate this code when closing the document or saving it. I tried copying / pasting the code into the Excel spreadsheet. I close the document / save it and nothing happens even with empty fields. I'm sure this is an easy answer. Could someone help? Thanks in advance.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim r As Range, txt As String
With Sheets("Sheet1")
For Each r In .Range("h4,h5")
If IsEmpty(r) Then
txt = txt & r.Address(0, 0) & vbLf
End If
Next
End With
If Len(txt) > 0 Then
MsgBox "You need to fill:" & vbLf & vbLf & txt
Cancel = True
End If
I found the following snippet of code (below) to check for required fields that are empty in an excel 2007 document. What I want to do is point out to the user that the fields are empty and are required to be filled in. I haven't tried it yet but reviews of the code seem to point that it will work for what I want to do. What I don't know how to do though is to activate this code when closing the document or saving it. I tried copying / pasting the code into the Excel spreadsheet. I close the document / save it and nothing happens even with empty fields. I'm sure this is an easy answer. Could someone help? Thanks in advance.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim r As Range, txt As String
With Sheets("Sheet1")
For Each r In .Range("h4,h5")
If IsEmpty(r) Then
txt = txt & r.Address(0, 0) & vbLf
End If
Next
End With
If Len(txt) > 0 Then
MsgBox "You need to fill:" & vbLf & vbLf & txt
Cancel = True
End If