I have to make a few hundred forms to various e-mail addresses, but I would like to prevent empty answers. I used "Data Validation" option (unchecked "ignore blank"), but you must enter the cell to activate this, if you don't enter you will not trigger validation.
I tried to find the VBA code and succeeded:
"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Set ws = Worksheets("A")
If ws.Application.WorksheetFunction.CountBlank(ws.Range("G5")) > 0 Then
MsgBox ("some text referring to enter in cell G5 in sheet A")
Cancel = True
End If
If ws.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet A")
Cancel = True
End If
Dim ws1 As Worksheet
Set ws1 = Worksheets("Dio B")
If ws1.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws1.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet B")
Cancel = True
End If
End Sub
"
But it is a Private Sub (Cancel As Boolean) and I can't spread it to all forms. I found code to do that, but it is not functioning if it is not "Private Sub(Cancel As Boolean)":
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
Dim ws As Worksheet
Set ws = Worksheets("Dio A")
If ws.Application.WorksheetFunction.CountBlank(ws.Range("G5")) > 0 Then
MsgBox ("some text referring to enter in cell G5 in sheet A")
Cancel = True
End If
If ws.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet A")
Cancel = True
End If
Dim ws1 As Worksheet
Set ws1 = Worksheets("Dio B")
If ws1.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws1.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet B")
Cancel = True
End If
End With
xFileName = Dir
Loop
End If
End Sub
I tried some other codes, but always the same: if it is not Private Sub (Cancel As Boolean) it is not functioning. Please help.
I tried to find the VBA code and succeeded:
"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Set ws = Worksheets("A")
If ws.Application.WorksheetFunction.CountBlank(ws.Range("G5")) > 0 Then
MsgBox ("some text referring to enter in cell G5 in sheet A")
Cancel = True
End If
If ws.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet A")
Cancel = True
End If
Dim ws1 As Worksheet
Set ws1 = Worksheets("Dio B")
If ws1.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws1.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet B")
Cancel = True
End If
End Sub
"
But it is a Private Sub (Cancel As Boolean) and I can't spread it to all forms. I found code to do that, but it is not functioning if it is not "Private Sub(Cancel As Boolean)":
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
Dim ws As Worksheet
Set ws = Worksheets("Dio A")
If ws.Application.WorksheetFunction.CountBlank(ws.Range("G5")) > 0 Then
MsgBox ("some text referring to enter in cell G5 in sheet A")
Cancel = True
End If
If ws.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet A")
Cancel = True
End If
Dim ws1 As Worksheet
Set ws1 = Worksheets("Dio B")
If ws1.Application.WorksheetFunction.CountBlank(ws.Range("Q12:Q12")) > 0 And ws1.Application.WorksheetFunction.CountBlank(ws.Range("T12:T12")) > 0 Then
MsgBox ("some text referring to enter in cell Q12 and T12 in sheet B")
Cancel = True
End If
End With
xFileName = Dir
Loop
End If
End Sub
I tried some other codes, but always the same: if it is not Private Sub (Cancel As Boolean) it is not functioning. Please help.