jski
Board Regular
- Joined
- Jan 11, 2006
- Messages
- 118
I’m constructing a VB code in a spreadsheet with the intent to force the user to complete certain required sections or they will be unable to print or close it. There are two high level issues I’m trying to solve with VB but I haven’t been too successful:
1.) If a certain cell is used (in this case H49) the user must click the sumbit button or they will be unable to close the spreadsheet. I think I’ve done this with the following code; however, I don’t think it is the most optimal:
2.) The second item is the user needs to complete a certain field (in this case C69). If they don’t, the user receives a message, the field is highlighted in yellow and they are unable to close the workbook. Here what I’ve done with this bit:
The problem I’m encountering is that each one of these bits of code counteracts the other. I’m at a point now where I’m totally unable to close the workbook. I think the two could be merged somehow to accommodate both needs. The end result would be that the user would be unable to close / save / print the workbook if cell H49 is used and the submit button was not clicked, and if cell C69 is left blank they would be unable to close /save / print as well. Finally, I’d like to force the user to Save As.
Guidance, instruction and general advice would be most helpful and appreciated. Thank in advance to all.
1.) If a certain cell is used (in this case H49) the user must click the sumbit button or they will be unable to close the spreadsheet. I think I’ve done this with the following code; however, I don’t think it is the most optimal:
Code:
Private Sub Workbook_Open()
'Sets the Submit button variable
Worksheets("TAP Form").Range("$A$999").Value = "CANT CLOSE"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Forces the user to select the Submit button if designated cell has a date in it
'before being able to close the spreadsheet
If Range("$H$49") > 0 Then
'If they have entered a date
If Worksheets("TAP Form").Range("$H$49").Value > "0" Then
'If they have not submitted the form
If Worksheets("TAP Form").Range("$A$999").Value = "CANT CLOSE" Then
'Doesn't let them close and gives them a message
Worksheets("TAP Form").Range("$H$49").Select
MsgBox "This TAP Form includes a financial plan." _
& vbCrLf & vbNewLine & "Please submit the form before closing."
Cancel = True
End If
End If
End If
End Sub
2.) The second item is the user needs to complete a certain field (in this case C69). If they don’t, the user receives a message, the field is highlighted in yellow and they are unable to close the workbook. Here what I’ve done with this bit:
Code:
Dim Start As Boolean
Dim Rng1 As Range
Dim Prompt As String, RngStr As String
Dim Cell As Range
'Set your ranges here
'Rng1 is on sheet "TAP Form" and is Cell C69
Set Rng1 = Sheets("TAP Form").Range("C69")
'Message is returned if there are blank cells
Prompt = "Please check your data ensuring all required " & _
"cells are complete." & vbCrLf & vbNewLine & "You will not be able " & _
"to close or save this workbook until you complete the required information." & _
vbCrLf & vbNewLine & _
"The following cells are incomplete and have been highlighted yellow:" _
& vbCrLf & vbNewLine & vbNewLine & vbNewLine
Start = True
'Highlights the blank cells
For Each Cell In Rng1
If Cell.Value = vbNullString Then
Cell.Interior.ColorIndex = 6 '** color yellow
If Start Then RngStr = RngStr & Cell.Parent.Name & vbCrLf
Start = False
'Displays the field name and cell number.
'The field name is a hard entry. Substitute RngStr for multiple fields
RngStr = "Market Value of Assets" & vbCrLf & vbNewLine & _
Cell.Address(False, False) & ", "
Else
Cell.Interior.ColorIndex = 0 '** no color
End If
Next
If RngStr <> "" Then RngStr = Left$(RngStr, Len(RngStr) - 2)
Start = True
If RngStr <> "" Then RngStr = RngStr & vbCrLf & vbNewLine
MsgBox Prompt & RngStr, vbCritical, "Incomplete Data"
Cancel = True
End If
Set Rng1 = Nothing
End Sub
The problem I’m encountering is that each one of these bits of code counteracts the other. I’m at a point now where I’m totally unable to close the workbook. I think the two could be merged somehow to accommodate both needs. The end result would be that the user would be unable to close / save / print the workbook if cell H49 is used and the submit button was not clicked, and if cell C69 is left blank they would be unable to close /save / print as well. Finally, I’d like to force the user to Save As.
Guidance, instruction and general advice would be most helpful and appreciated. Thank in advance to all.
Last edited by a moderator: