Mandatory field in range based on previous inputs

kubabocz

New Member
Joined
Oct 28, 2015
Messages
36
Hi I have tried lots of diffferent variations but I can not make it work. I would really appreciate if someone could help me with my macro.

I have a spreadsheet where people will populate some of the cells in column J. Then based on input in column J I want the fields in column K to become mandatory. For example: if someone write anything in J12 I want K12 to be mandatory, if someone puts anything in J23, K23 should be mandatory. I need it to work for the whole range as spreadsheet have about 1000 lines.

Thank you and I hope that someone will be able to help.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
By "mandatory" what behavior are you expecting? Should the user not be able to save without filling in these fields? Should they not be able to navigate away from a mandatory field? Should they get a pop up message telling them an input is required?
 
Upvote 0
Hi I have tried lots of diffferent variations but I can not make it work. I would really appreciate if someone could help me with my macro.

I have a spreadsheet where people will populate some of the cells in column J. Then based on input in column J I want the fields in column K to become mandatory. For example: if someone write anything in J12 I want K12 to be mandatory, if someone puts anything in J23, K23 should be mandatory. I need it to work for the whole range as spreadsheet have about 1000 lines.

Thank you and I hope that someone will be able to help.
Hi kubabocz, welcome to the boards.

The following Worksheet_Change event code can probably do what you are after. It is added directly to the backend of the sheet in question rather than to a standard VBA module. To do this, simply right-click on the sheet name and select View Code. The following code can then be copy and pasted in to the VBA window that comes up:

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
' If the target column is greater than J And J isn't blank And K is blank then...
If Target.Column > 10 And Range("J" & Target.Row).Value <> "" And Range("K" & Target.Row).Value = "" Then
' Display error message to this effect
    MsgBox "Error! Please complete column K before moving on"
' Disable events to prevent infinite loop
        Application.EnableEvents = False
' Clear the contents of the target cell
            Target.ClearContents
' Re-enable events
                Application.EnableEvents = True
' Select column K of the target row
                    Range("K" & Target.Row).Select
End If
End Sub
 
Upvote 0
Hi, thank you very much for your help, unfortunately it's not exactly what I needed
.
With this code it gives youe message after every input in column J straight away, without even giving a chance to put something in column K. I was thinking that it would be better if it could flag the missing inputs in column K on Saving the file.
Second thing is that after seeing the message you can still leave the cell in column K blank.

It's very close to the ideal solution and I would be very grateful if you could try to make it work as per my comments above.

Thank you
Kubabocz
 
Upvote 0
Hi, thank you very much for your help, unfortunately it's not exactly what I needed
.
With this code it gives youe message after every input in column J straight away, without even giving a chance to put something in column K. I was thinking that it would be better if it could flag the missing inputs in column K on Saving the file.
Second thing is that after seeing the message you can still leave the cell in column K blank.

It's very close to the ideal solution and I would be very grateful if you could try to make it work as per my comments above.

Thank you
Kubabocz
OK whilst I couldn't seem to recreate your issue at this end, I have been able to change how this works more to your new requirements. The new macro is a Workbook_BeforeSave event instead of a Worksheet_Change event.

Firstly make another COPY of your workbook to test this out on.
Secondly you will need to go into the backend of your sheet and delete my original code.
Next, you will need to find your workbook on the left hand side in the VBA developer window, right-click on ThisWorkbook and select View Code. You can then copy and paste in the following code:

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' Defines variables
Dim Cell, cRange As Range
' Defines LastRow as last row of column J with a value
LastRow = ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row
' Defines range to check
    Set cRange = Range("K2:K" & LastRow)
' For each cell in range
        For Each Cell In cRange
' If column K is blank but columns J and L are not blank then...
            If Cell.Value = "" And Cell.Offset(0, -1).Value <> "" And Cell.Offset(0, 1) <> "" Then
' Highlight the cell in column K with a yellow fill
                Cell.Interior.ColorIndex = 6
' Else If columns J and K are not blank then...
            ElseIf Cell.Value <> "" And Cell.Offset(0, -1).Value <> "" Then
' Remove the yellow fill from column K
                Cell.Interior.ColorIndex = xlNone
            End If
' Check next cell in range
        Next Cell
' If the count of filled cells in column K is less than the count of filled cells in column L then...
            If Application.WorksheetFunction.CountIf(Range("K2:K" & LastRow), "<>") < Application.WorksheetFunction.CountIf(Range("L2:L" & LastRow), "<>") Then
' Display an error message stating saving will not happen unless all highlighted cells must be filled in
                    MsgBox "Workbook will not be saved unless" & vbCrLf & _
                        "All highlighted cells have been filled in!"
' Cancel Save function
                            Cancel = True
            End If
End Sub

A breakdown of what this new code is doing is as follows:

When you try and save the document...

- It checks the adjacent cells of any blank cells in column K. If K is blank but L and J are not (meaning K has been skipped), highlight the blank cell in column K yellow.
- If column J is filled in but columns K and L are blank, do nothing as technically K hasn't been skipped yet (L needs to be filled in for K to have been skipped).
- Throws out an error message that all highlighted cells must be completed or saving cannot happen.
- Cancels the Save function.

If however you then fill in the highlighted cells as required and then click Save again...

- It checks the adjacent cells of column K and if J, K and L are no longer blank (meaning K has NOT been skipped), remove the yellow highlight.
- Allows the Save function to complete
 
Upvote 0
Hi, thank you for trying to help I checked it many times and it doesn't do anything. Also could you please try to stick just to two columns (if J is filled but K is empty then flag it). Sorry to be the pain in the *** but it would help me with many different workbooks
 
Upvote 0
Hi, thank you for trying to help I checked it many times and it doesn't do anything. Also could you please try to stick just to two columns (if J is filled but K is empty then flag it). Sorry to be the pain in the *** but it would help me with many different workbooks
Here is the test workbook I created the code in. If it works for you on this but not on your workbook, you have done something wrong. Please test it out and let me know.
 
Upvote 0
Seems to be working fine, now I will just have to amend it so it only looks at 2 columns, thank you very much for help :)
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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