Should Cell Validation Trap Null Entries?

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a cell ("G3") that has a list cell validation associated to it. If the user deletes the default value and doesen't select another value, tabs out or clicks elsewhere, the worksheet change event triggers. Because the code relies on a value being in that cell, it errs. Does the fact that I have a cell validation limiting the user to only certain values not prevent the user from proceeding without a value in the cell?

Rich (BB code):
]
If Not mbevents Then Exit Sub
     'If not Target.Address = "$G$3" as nothing Then
     If Not Intersect(Target, Range("$G$3")) Is Nothing Then
        dSDay = Target.Value
        dSMonth = Format(Month(DateValue("1 " & Range("E3") & " 2020")), "00")
        dSYear = Range("C3").Value
        dNDate = DateSerial(dSYear, dSMonth, dSDay) 'dSDay is empty ... it shouldn't have got this far?
        mbevents = False
        Unprotect
        Range("C4") = UCase(Format(dNDate, "DDDD"))
        Range("C" & dcomprow) = Format(dNDate, "dd-mmm-yy")
        mbevents = True
        Protect
     End If
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Just check to see that there is a value in there, i.e.
amend this line:
Rich (BB code):
If Not Intersect(Target, Range("$G$3")) Is Nothing Then
to this:
Rich (BB code):
If (Not Intersect(Target, Range("$G$3")) Is Nothing) And (Target <> "") Then
 
Upvote 0
Hi Joe. Thank you for sharing a new concept with me. I didn't realize I could do that. I can use that in so many places now.
I am however getting a "type mismatch" error when this line is encountered.
Code:
If (Not Intersect(Target, Range("$G$3")) Is Nothing) And (Target <> "") Then
 
Upvote 0
Hi Joe. Thank you for sharing a new concept with me. I didn't realize I could do that. I can use that in so many places now.
I am however getting a "type mismatch" error when this line is encountered.
Code:
If (Not Intersect(Target, Range("$G$3")) Is Nothing) And (Target <> "") Then
Maybe try splitting it over two lines instead, i.e.
VBA Code:
If Not Intersect(Target, Range("$G$3")) Is Nothing Then
    If Range("$G$3") = "" Then Exit Sub
 
Upvote 0
Solution
Hi Mike ...
When setting up the Validation, un-check the IgnoreBlanks in the dialog.
My list cell validation is created and applied with VBA as the list is dynamic. I didn't use the Excel dialogue for that. Can that setting ("ignoreBlanks") be coded?

Maybe try splitting it over two lines instead, i.e.
VBA Code:
If Not Intersect(Target, Range("$G$3")) Is Nothing Then
    If Range("$G$3") = "" Then Exit Sub

Hi Joe. Yes ... this works. Thank you!
 
Upvote 0
Hi Mike ...

My list cell validation is created and applied with VBA as the list is dynamic. I didn't use the Excel dialogue for that. Can that setting ("ignoreBlanks") be coded?
I got this from the Macro Recorder
Rich (BB code):
With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="cat,dog,Charlie"
        .IgnoreBlank = False
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
 
Upvote 0
Hi Mike ... I'm interested in your code as it provided an answer to what was to be my next question. (customizing the error message).

I have added your suggested code. If I enter an invalid value, the error is reported. However, if I delete the value in the cell and tab out, the error isn't reported. "IgnoreBlank" is indeed set to false, which if I understand correctly, means that if the cell is empty, that the validation won't be ignored and as a blank is not part of the list values, will trigger the error. This isn't happening in my case. It seems as though the blank cell is being accepted.

Then I realized that I was still using Joe4's suggestion from post 5, which was trapping that error any exiting the sub. (this works but I haven't figured out how to return the original value to the cell ... ie cancel).

So, with Joe4's code commented out, when I delete the value in the cell and tab out, I get an error as the change event for that cell triggers assuming a valid value (which of course is not ... it's blank).

Am I misunderstanding the behaviour of "ignoreBlank"? Do I still need Joe4's code? And if so, what to modify in it to return that cell's value back to the previous value.
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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