remove date if

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
675
Office Version
  1. 365
Platform
  1. Windows
hello & good evening,

I use the following code:-

Private Sub Worksheet_Change(ByVal Target As Range)


ActiveSheet.Unprotect


Dim rng As Range
Dim rng2 As Range
Dim cell As Range
Dim r As Long
Dim c As Long

Set rng = Intersect(Target, Range("E2:G200"))

' Exit sub if no cells updated in range
If rng Is Nothing Then Exit Sub

Application.EnableEvents = False

' Loop through updated cells in range
For Each cell In rng
' Get row and column number of updated cell
r = Target.Row
c = Target.Column
' Count how many cells have "Y" in current row
Set rng2 = Range("E" & r & ":G" & r)
If Application.WorksheetFunction.CountIf(rng2, "Y") > 1 Then
' Clear entry
cell.ClearContents
' Return message
MsgBox "You can put one Y in cell range E-G " & cell.Address(0, 0), vbOKOnly, "ERROR!"
Else
' See which column was updated and make appropriate adjustments
If LCase(cell) = "y" Then
Select Case c
' What to do if column E updated to "y"
Case 5
'enter any desired code here
' What to do if column F updated to "y"
Case 6
'enter any desired code here
' What to do if column G updated to "y"
Case 7
With Cells(r, "B")
.NumberFormat = "dd/mm/yyyy"
.Value = Date
End With
End Select
End If
End If
Next cell

Application.EnableEvents = True

ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True


End Sub

This works but the issue I have is that when I remove the 'Y' from cell G2.. the date remains in cell B2.

Can some kind and more knowledgeable person solve this for me please?

Many thanks in advance & I thank you for your time & assistance to day
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
hello & good evening,

I use the following code:-

Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_Change(ByVal Target As Range)
    
    
    ActiveSheet.Unprotect
   
  
    Dim rng As Range
    Dim rng2 As Range
    Dim cell As Range
    Dim r As Long
    Dim c As Long
  
    Set rng = Intersect(Target, Range("E2:G200"))
    
'   Exit sub if no cells updated in range
    If rng Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    
'   Loop through updated cells in range
    For Each cell In rng
'       Get row and column number of updated cell
        r = Target.Row
        c = Target.Column
'       Count how many cells have "Y" in current row
        Set rng2 = Range("E" & r & ":G" & r)
        If Application.WorksheetFunction.CountIf(rng2, "Y") > 1 Then
'           Clear entry
            cell.ClearContents
'           Return message
            MsgBox "You can put one Y in cell range E-G  " & cell.Address(0, 0), vbOKOnly, "ERROR!"
        Else[B][COLOR="#FF0000"]If Application.WorksheetFunction.CountIf(rng2, "Y") = 1 Then[/COLOR][/B]
'           See which column was updated and make appropriate adjustments
            If LCase(cell) = "y" Then
                Select Case c
'                   What to do if column E updated to "y"
                    Case 5
                        'enter any desired code here
'                   What to do if column F updated to "y"
                    Case 6
                        'enter any desired code here
'                   What to do if column G updated to "y"
                    Case 7
                        With Cells(r, "B")
                            .NumberFormat = "dd/mm/yyyy"
                            .Value = Date
                        End With
                End Select
            End If
[B]        [B][COLOR="#FF0000"]Else
            Cells(r, "B").Value = ""[/COLOR][/B]
[/B]        End If
    Next cell
    
    Application.EnableEvents = True
    
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True


End Sub[/td]
[/tr]
[/table]

This works but the issue I have is that when I remove the 'Y' from cell G2.. the date remains in cell B2.
(Untested) Do the additions that I show in red above make the code work the way you want?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,104
Messages
6,170,126
Members
452,303
Latest member
c4cstore

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