PDA

View Full Version : Solved: Required Cells Before User Can Save activated by a trigger



valendj
08-16-2010, 06:58 AM
Hi,
I am working on a code so the users of the spreadsheet have to fill out all required fields before saving but, I need it to have a trigger if someone imputed data into A1 then that row would have the required fields. Then the next user would input data in A2 and then that row would have the required fields. Up to now all I can figure out is certain cells required. I have been looking through past vbax forum entries and I found a few but, not exactly what I am looking for. Even found one that highlighted the fields the user forgot after the message box appeared which would be great but, one obstacle at a time.

Any help greatly appreciated!
:banghead:

Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not CheckDateField Then
MsgBox "You Must Fill Out All Highlighted Fields"
Cancel = True
End If
End Sub

Private Function CheckDateField() As Boolean
CheckDateField = False
If Worksheets(1).Range("B2").Value <> "" Then
CheckDateField = True
End If
End Function

Private Function CheckDateFiel() As Boolean
CheckDateField = False
If Worksheets(1).Range("D2").Value <> "" Then
CheckDateField = True
End If
End Function

Bob Phillips
08-16-2010, 09:23 AM
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A:A" '<<<< change to suit

On Error GoTo ws_exit

Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then

With Target

If Me.Cells(.Row, "B") = "" Or Me.Cells(.Row, "D") = "" Then

MsgBox "You Must Fill Out All Highlighted Fields"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub


This is worksheet event code, which means that it needs to be
placed in the appropriate worksheet code module, not a standard
code module. To do this, right-click on the sheet tab, select
the View Code option from the menu, and paste the code in.

valendj
08-16-2010, 09:47 AM
This does not work. I get the "Must Fill out all fields" message once I click out of the cell and it still allows me to save.

Bob Phillips
08-16-2010, 10:13 AM
You have to trap the BeforeSave as well, this just traps the conditions on input as I thought you were asking for.

valendj
08-16-2010, 10:42 AM
Clarification
What I was looking for was when the user clicks on any cell in column and it auto fills with a unique identifier which you helped me with (Thanks Again!). That would be the trigger to activate which cells in each column /row that need to be required and if those cells are empty then a message will be generated and the user will not be allowed to save his idea. I can do it for specific cells that block the user from saving but, I cannot make the required cells move down the sheet with each new entry.