color cell in red, when entering empty time value

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
339
Office Version
  1. 2016
Platform
  1. Windows
I have a range of cells in column B and C, where the user has to enter some time values. If he accidentally enters an empty value, then this is trapped by the below code. However, I want to add some color as long as the user does enter empty values.

Thanks for any help.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim col_B_rng As Range
Dim col_C_rng As Range

Dim StartTime As String
Dim EndTime As String

Dim startNum As Integer
Dim endNum As Integer

Set col_B_rng = Range("B2:B4")
Set col_C_rng = Range("C2:C4")


If Target.Count = 1 And Not Application.Intersect(Target, col_B_rng) Is Nothing Then

    StartTime = Target.Value
    Application.EnableEvents = False
        
    If StartTime = "" Then
        Target.Select
        MsgBox ("Empty value.  Try again."), vbCritical
        Application.Undo
        Application.EnableEvents = True
        Exit Sub
    Else
        StartTime = Format(Target.Value, "0\:00")
        Target.Offset(0, 1).Select
    End If
End If
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Why not use a conditional formatting rule applied to B2:B4 to apply a fill color when the cell is blank:
Excel Formula:
=B2=""

Also your code defines col_C_rng but never uses it. Did you mean to include those cells too?
Rich (BB code):
If Target.Count = 1 And (Not Application.Intersect(Target, col_B_rng) Is Nothing Or Not Application.Intersect(Target, col_C_rng) Is Nothing) Then
 
Upvote 0
Thanks. I only posted part of my code, Col C is used further on.

I am not very pro conditional formatting...
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

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