View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Force input in multiple cells in Excel form

Are you sure you want to abort the save? Not saved is not saved and from my
experience you are going to get lots of compalints that the users lost their
input or they will enter a lot of garbage to allow the save. I personally
prefer a warning the the file is incomplete when they try to save but to
allow the save to continue anyway... On exiting the file the same check
should occure and allow the user to abort the exit... Just my 2 cents. If you
want help with that the code is not too difficult...

Something like this should be close (untested)...

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim rngMissed As Range

Set rngMissed = AllComplete
If Not rngMissed Is Nothing Then
If MsgBox("Close the file?", vbYesNo) = vbNo Then
Cancel = True
rngMissed.Parent.Activate
rngMissed.Select
End If
End If
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rngMissed As Range

Set rngMissed = AllComplete
If Not rngMissed Is Nothing Then
If MsgBox("Close the file?", vbYesNo) = vbNo Then
Cancel = True
rngMissed.Parent.Activate
rngMissed.Select
End If
End If
End Sub


Private Function AllComplete() As Range

Set AllComplete = Nothing

With Sheets("Sheet1")
If Trim(.Range("E10")) = "" Then
Set AllComplete = .Range("E10")
MsgBox ("Missed this one")
ElseIf Trim(.Range("E11")) = "" Then
Set AllComplete = .Range("E11")
MsgBox ("Missed this one")
End If
End Function
--
HTH...

Jim Thomlinson


"Al" wrote:

Sorry, I forgot to mention that the Data Validation option is grayed out and
not available.

Thanks again,
Al

"Ryan H" wrote:

I think for what you are doing, you don't need VB. You can just use Data
Validation. Just Select the Cell you want to have data validated, put in
your parameters, message Title and Input message, and other settings if you
like. Then if the user doesn't meet your specifications the message box will
pop-up.

Hope this helps! If so, let me know, click "YES" below.
--
Cheers,
Ryan


"Al" wrote:

I have tried several different things to make this work and need professional
help. I have an Excel form with several fields (11 all together) that
require user input before it can be or should be allowed to be saved.
The first code I tried, and I was new at this with Excel, was to try
multiple actions like the following (which did not work) Oh, I should say
that it worked for the very first cell but none after that:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("E10").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E10").Select
Application.EnableEvents = True
MsgBox ("You must enter a value for 'Department Name'")
End If
End Sub

Private Sub Worksheet_SelectionChange1(ByVal Target As Range)
If Range("E11").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E11").Select
Application.EnableEvents = True
MsgBox ("You must enter a value for 'Address'")
End If
End Sub

Private Sub Worksheet_SelectionChange2(ByVal Target As Range)
If Range("E12").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E12").Select
Application.EnableEvents = True
MsgBox ("You must enter a value for 'Contract Type'")
End If
End Sub
.
.
.
Then I realized it was not working because I was changing the very name of
the action that made it work "Worksheet_SelectionChange" so I tried nesting
the If statements, which also did not work:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Excel.ActiveCell = E10 Then
If Range("E10").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E10").Select
Application.EnableEvents = True
MsgBox ("Please enter a Department Name")
End If
Else
If Excel.ActiveCell = E11 Then
If Range("E11").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E11").Select
Application.EnableEvents = True
MsgBox ("Please enter an Address")

End If

Else
If Excel.ActiveCell = E12 Then
If Range("E12").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E12").Select
Application.EnableEvents = True
MsgBox ("Please enter a 'Contract Type'")
End If

Else
If Excel.ActiveCell = E13 Then
If Range("E12").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E12").Select
Application.EnableEvents = True
MsgBox ("Please enter a 'Contract Document Type'")
End If

Else
If Excel.ActiveCell = E14 Then
If Range("E12").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E12").Select
Application.EnableEvents = True
MsgBox ("Please enter a Contractor")
End If

Else
If Excel.ActiveCell = E15 Then
If Range("E12").Value = "" Then
On Error Resume Next
Application.EnableEvents = False
Range("E12").Select
Application.EnableEvents = True
MsgBox ("Please enter a Contractor Address")
End If
.
.
.
Ok, so I thought I need to simplify this and went to a Select Case
statement, which of course also does not work:
Private Sub Contract_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Select Case Excel.Range
Case "E10" = ""
MsgBox "Please enter enter a Department Name"
Cancel = True 'cancels the save event
Case "E11" = ""
MsgBox "Please enter an Address"
Cancel = True 'cancels the save event
Case "E12" = ""
MsgBox "Please enter a 'Contract Type'"
Cancel = True 'cancels the save event
Case "E13" = ""
MsgBox "Please enter a 'Contract Document Type'"
Cancel = True 'cancels the save event
Case "E14" = ""
MsgBox "Please enter a Contractor"
Cancel = True 'cancels the save event
Case "E15" = ""
MsgBox "Please enter a Contractor Address"
Cancel = True 'cancels the save event
Case "E17" = ""
MsgBox "Please enter a Project Title"
Cancel = True 'cancels the save event
Case "O10" = ""
MsgBox "Please enter a Contact Name"
Cancel = True 'cancels the save event
Case "O11" = ""
MsgBox "Please enter a Telephone Number"
Cancel = True 'cancels the save event
Case "A23" = ""
MsgBox "Please enter a Summary"
Cancel = True 'cancels the save event
Case "A44" = ""
MsgBox "Please enter a Request for Action Description"
Cancel = True 'cancels the save event
End Select
End Sub

I am hoping that someone has seen or done something like this and can tell
me that this is possible. I really appreciate any help you can provide.

Thank you in advance,
Al