Required Fields

hinesgg

New Member
Joined
Jul 11, 2012
Messages
30
I have a workbook containing a sheet requiring data in specific fields before the user can close the workbook. However, I need for users to be able to copy the sheet to create new ones and have Excel stop the close if they don't enter data in the same cells on the new sheet.

Right now the code I'm using works, but if I copy the sheet, Excel does not stop the close if data is missing is on the new sheet.

Here's the code I'm using now. What can I use instead of specifically naming the sheet so Excel will recognize the code when the sheet is copied? Also, is there something (wild card operator to represent the sheet name) I can add to the pop-up message to tell the user which tab/sheet the data is missing from?

Private Sub Workbook_BeforeClose(Cancel As Boolean)

If Worksheets("RequestForm-Template").Range("D4").Value = "" Then
MsgBox "You must fill in D4."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D6").Value = "" Then
MsgBox "You must fill in D6."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D8").Value = "" Then
MsgBox "You must fill in D8."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D11").Value = "" Then
MsgBox "You must fill in D11."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D13").Value = "" Then
MsgBox "You must fill in D13."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D16").Value = "" Then
MsgBox "You must fill in D16."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D18").Value = "" Then
MsgBox "You must fill in D18."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D20").Value = "" Then
MsgBox "You must fill in D20."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D22").Value = "" Then
MsgBox "You must fill in D22."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D24").Value = "" Then
MsgBox "You must fill in D24."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D26").Value = "" Then
MsgBox "You must fill in D26."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D30").Value = "" Then
MsgBox "You must fill in D30."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D32").Value = "" Then
MsgBox "You must fill in D32."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D34").Value = "" Then
MsgBox "You must fill in D34."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D36").Value = "" Then
MsgBox "You must fill in D36."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D38").Value = "" Then
MsgBox "You must fill in D38."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D40").Value = "" Then
MsgBox "You must fill in D40."
Cancel = True
End If
If Worksheets("RequestForm-Template").Range("D42").Value = "" Then
MsgBox "You must fill in D42."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D45").Value = "" Then
MsgBox "You must fill in D45."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D47").Value = "" Then
MsgBox "You must fill in D47."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D50").Value = "" Then
MsgBox "You must fill in D50."
Cancel = True
End If

If Worksheets("RequestForm-Template").Range("D52").Value = "" Then
MsgBox "You must fill in D52."
Cancel = True
End If

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Am no VBA expert so I don't know if this works, try it.

Store the sheet name in a cell on the sheet,e.g. in Z1
Then change all the Worksheets references to

Code:
Worksheets(Range("Z1")).Range("D4")

Not sure about the wild card request.
 
Last edited:
Upvote 0
This should work though I only tested in an ordinary sub()

Code:
shtnm=Range("Z1")
Worksheets(shtnm).Range("D4")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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