insert date based on cell date

Trevor3007

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

i am struggling to solve my issue & hope some 'code wizard' can solve?

My issue is , if cell a2 > 0, then insert todays date. So i need a formula for that and if I use Now() the date will change all applicable cells the following day . I need the original date to stay etc.

thank you for your help & for your time today.

KR
Trevor3007
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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:H200"))
    
'   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 & ":H" & 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-H  " & cell.Address(0, 0), vbOKOnly, "ERROR!"
        ElseIf Application.WorksheetFunction.CountIf(rng2, "Y") = 1 Then
'           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
                        'enter any desired code here
'                   What to do if column H updated to "y"
                    
                    Case 8
                        With Cells(r, "B")
                            .NumberFormat = "dd/mm/yyyy"
                            .Value = Date
                        End With
                End Select
            End If
        Else
            Cells(r, "B").Value = ""
        End If
    Next cell
    
    Application.EnableEvents = True
    
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True

End Sub


is that what you require?
sorry if not, I have not done this B4
 
Upvote 0
Paste this code in any module in your workbook, manually run it, and tell me what it returns:
Code:
Sub AreEventsEnabled()
    MsgBox "It is " & Application.EnableEvents & " that events are enabled."
End Sub
Also, is column B on your "results" sheet protected, or part of a merged cell?
 
Last edited:
Upvote 0
hi

i pasted as requested into 'results' worksheet & it returned:-
it is true all event are enabled..

I don't have your other code in any worksheet ATM , BTW
:pray:

PS

is column B on your "results" sheet protected, or part of a merged cell?

protected


 
Last edited:
Upvote 0
hi

i pasted as requested into 'results' worksheet & it returned:-
it is true all event are enabled..

I don't have your other code in any worksheet ATM , BTW
:pray:

PS

is column B on your "results" sheet protected, or part of a merged cell?

protected
 
Upvote 0
is column B on your "results" sheet protected, or part of a merged cell?

protected
That is critical information! Please be sure to mention such important details.
We obviously cannot update a protected sheet unless we unprotect it first!

Here is all your code, rolled into one procedure.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   ********** CODE BLOCK 1 **********
    Dim rng1 As Range
    Dim cell1 As Range
    
'   Check to see if any cells updated in range G2:G200 on "testdata" sheet
    Set rng1 = Intersect(Target, Range("G2:G200"))
    
'   Exit if no cells updated in that range
    If rng1 Is Nothing Then Exit Sub
    
'   Loop through updated cells
    If Not rng1 Is Nothing Then
'       Unprotect results sheet
        Sheets("results").Activate
        ActiveSheet.Unprotect
        For Each cell1 In rng1
'           If "Y" entered in column G, ...
            If UCase(cell1) = "Y" Then
'               then update column B of same row on "result" sheet
                Sheets("results").Cells(cell1.Row, "B") = Now()
            End If
        Next cell1
'       Reprotect sheet
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True
        Sheets("testdata").Activate
    End If
    

'   ********** CODE BLOCK 2 **********
    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:H200"))
    
'   Exit sub if no cells updated in range
    If rng Is Nothing Then Exit Sub
    
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    
'   Loop through updated cells in range
    For Each cell In rng
'       Get row and column number of updated cell
        r = cell.Row
        c = cell.Column
'       Count how many cells have "Y" in current row
        Set rng2 = Range("E" & r & ":H" & 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-H  " & cell.Address(0, 0), vbOKOnly, "ERROR!"
        ElseIf Application.WorksheetFunction.CountIf(rng2, "Y") = 1 Then
'           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
                        'enter any desired code here
'                   What to do if column H updated to "y"
                    
                    Case 8
                        With Cells(r, "B")
                            .NumberFormat = "dd/mm/yyyy"
                            .Value = Date
                        End With
                End Select
            End If
        Else
            Cells(r, "B").Value = ""
        End If
    Next cell
    
    Application.EnableEvents = True
    
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True
    
    
End Sub
 
Upvote 0
thanks again,


all seemed great but:

in my original code if any cell with in e-h had 2 x y it would issue a message:-

You can put one Y in cell range E-H and displays the applicable 2nd Y cell ref and removes the 2 Y , when your code is included it does not display the error message

also, when the Y is removed, it does not remove the date from the range b2:b200 on the results worksheet. and I noticed that you are using now() within your code, would not date change when the file is used again on another day ? (IE all previous work done on 21/8/18 would change to the IE 22/8/18?)

The original date must remain as they are used for daily/weekly management statistics

so sorry to send you disappointing news :{ but I do appreciate all your hard work of course :)
 
Last edited:
Upvote 0
You can put one Y in cell range E-H and displays the applicable 2nd Y cell ref and removes the 2 Y , when your code is included it does not display the error message
Sorry, I forgot to remove the part about exit the sub at the top of the code. I have amended that.

also, when the Y is removed, it does not remove the date from the range b2:b200 on the results worksheet.
You didn't mention that as a requirement. I have updated the code so that if the value in column G is removed, the corresponding time stamp in column B on the "results" will be removed.

I noticed that you are using now() within your code, would not date change when the file is used again on another day ? (IE all previous work done on 21/8/18 would change to the IE 22/8/18?)
No, I have explained this twice already (in post 8 and 10). Using NOW() in VBA is NOT the same as using =NOW() on the spreadsheet. If you don't believe me, it is really easy to prove to yourself.
Update cell G2 to "Y", and then take a look at the contents of cell B2 on the "results" sheet. Do you see the "=NOW()" formula in that cell, or do you see a hard-coded date/time?
Now, save your file, wait a few minutes, and open it back up. Check cell B2 on the "results" sheet again. Has the time changed? You will see that it doesn't.

The only way that date/time will be updated again is if you type "Y" into column G on the "testdata" sheet again.
If you like, we can add a further check that says if there is already a time stamp in column B on the "results" sheet, do not update it if they accidentally enter "Y" again in column G on the "testdata" sheet.

I also changed the order of these two things in the VBA code to make the two things work a little better. Here is the latest version of the code.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


'   ********** CODE BLOCK 1 **********
    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:H200"))
    
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    
    If Not rng Is Nothing Then
'       Loop through updated cells in range
        For Each cell In rng
'           Get row and column number of updated cell
            r = cell.Row
            c = cell.Column
'           Count how many cells have "Y" in current row
            Set rng2 = Range("E" & r & ":H" & 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-H  " & cell.Address(0, 0), vbOKOnly, "ERROR!"
            ElseIf Application.WorksheetFunction.CountIf(rng2, "Y") = 1 Then
'               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
                            'enter any desired code here
'                       What to do if column H updated to "y"
                        Case 8
                            With Cells(r, "B")
                                .NumberFormat = "dd/mm/yyyy"
                                .Value = Date
                            End With
                    End Select
                End If
            Else
                Cells(r, "B").Value = ""
            End If
        Next cell
    
        Application.EnableEvents = True
    
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True
        
    End If
    

'   ********** CODE BLOCK 2 **********
    Dim rng1 As Range
    Dim cell1 As Range
    
'   Check to see if any cells updated in range G2:G200 on "testdata" sheet
    Set rng1 = Intersect(Target, Range("G2:G200"))
    
'   Loop through updated cells
    If Not rng1 Is Nothing Then
'       Unprotect results sheet
        Sheets("results").Activate
        ActiveSheet.Unprotect
        For Each cell1 In rng1
            Select Case UCase(cell1)
'               Add date stamp to column B on "results" sheet if "Y" added to column G
                Case "Y"
                    Sheets("results").Cells(cell1.Row, "B") = Now()
'               Clear date stamp from column B on "results" if column G changed to blank
                Case ""
                    Sheets("results").Cells(cell1.Row, "B").ClearContents
            End Select
        Next cell1
'       Reprotect sheet
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True
        Sheets("testdata").Activate
    End If
    
    
End Sub
 
Upvote 0
thank you for all your hard work.


As for the date remaining, actully this is OK and will be an asset to the stats :}


However ….

a couple of issues unfortunately

when I place 2 x Y in any cell between E2:H200 range I don't get the error message as before?
and on the testdata sheet, placing a Y in any cell within h2:h200 range it should (as previously) trigger a date stamp in the range b2:b200.

KR
Trevor
 
Last edited:
Upvote 0
thank you for all your hard work.


As for the date remaining, actully this is OK and will be an asset to the stats :}


However ….

a couple of issues unfortunately

when
I place 2 x Y in any cell between E2:H200 range (IE e2 & f2) I don't get the error message as before (testdata)?
and also the testdata sheet, placing a Y in any cell within h2:h200 range (ie h2) it should (as previously) trigger a date stamp in the range b2:b200 (ie b2).

KR
Trevor

PS
sorry I should have added this in my previous post to you & realised after allowed 10min edit limit
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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