insert date based on cell date

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
667
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
 
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)?
Have you tried the latest code I posted? It works for me.
On the "testdata" sheet, if I enter "Y" in E18 and then "Y" in F18, I get the message: "You can put one T in the cell range E-H F18", and it removes the second one.

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).
Everything we can have been working on up to this point has been for column G, not column H. Are you changing the requirements?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
hi,

Everything we can have been working on up to this point has been for column G, not column H. Are you changing the requirements?

no... the cells/range are as I specified are correct. Col B & H were not part of the issue so I did not need to change (col B & H (testdata sheet) is in in the code I sent to you however.


On the "testdata" sheet, if I enter "Y" in E18 and then "Y" in F18, I get the message: "You can put one T in the cell range E-H F18", and it removes the second one.

If I open the file and add 2 x Y in IE e2 & f2 the message appears. but if I use the macro to clear the data , and repeat same in E2 & F2 no message.

The workbook has been made so the end user has a minimal amount of input & max amount of output.




 
Last edited:
Upvote 0
sorry should of included in my last:-

see within my VB:-

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
'

and :-



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

 
Upvote 0
Are you saying that you want the date/time stamp piece to actually work when ANY cell in columns E-H is updated to "Y" (not just column G)?
So if cell E4 was updated to "Y" on the "testdata" sheet, the date/time stamp on the "results" sheet in cell B4 should be updated?
Based on your original posting, it was my understanding that the date/time stamp piece only run when column G was updated, but it sounds like maybe you want any column in G-H.
Is that correct?

If I open the file and add 2 x Y in IE e2 & f2 the message appears. but if I use the macro to clear the data , and repeat same in E2 & F2 no message.
Do you have some other macro you are running to clear the code?
If so, what does that code look like?

When quoting me in your replies, please use the "quote" tags to avoid confusion and make it much easier to read (like I did above). All you have to do is highlight the quotes text after pasting it, and click on the "speech bubble" icon in the editor tool bar. It is the second to last icon, just before the code tag.
 
Upvote 0
Are you saying that you want the date/time stamp piece to actually work when ANY cell in columns E-H is updated to "Y" (not just column G)?
So if cell E4 was updated to "Y" on the "testdata" sheet, the date/time stamp on the "results" sheet in cell B4 should be updated?
Based on your original posting, it was my understanding that the date/time stamp piece only run when column G was updated, but it sounds like maybe you want any column in G-H.
Is that correct?


Do you have some other macro you are running to clear the code?
If so, what does that code look like?

When quoting me in your replies, please use the "quote" tags to avoid confusion and make it much easier to read (like I did above). All you have to do is highlight the quotes text after pasting it, and click on the "speech bubble" icon in the editor tool bar. It is the second to last icon, just before the code tag.


Hi Joe4

many thanks for your email and your great help with my issue.

on the testdata worksheet the only time a date should appear is when a Y is placed in any relevant cell in the H column which triggers and cell in B column. All other times there is no date stamp displayed within the testdata worksheet.


Still on the testdata worksheet. When a Y is placed in any applicable cell (G2-G200) in col G this triggers a date stamp on the results worksheet.

Col B (b2-b200).


The code that deletes applicable data from the testdata worksheet is:-
Code:
Sub WipeTheSlate()
'
' WipeTheSlate Macro
'
    ActiveSheet.Unprotect
    

If MsgBox("This will clear all data in Columns A-B & E-I. Press Yes to delete or No to stop.", vbYesNo + vbQuestion) = vbNo Then Exit Sub
    
 
    Range("A2:A200").Select
    Selection.ClearContents
    Range("B2:B200").Select
    Selection.ClearContents
    Range("E2:I200").Select
    Selection.ClearContents
    Range("A2").Select
    
   ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True
        
               
    
End Sub


[LEFT][COLOR=#222222][FONT=Verdana]


hope this helps & thank so much[/FONT][/COLOR]




[/LEFT]
 
Upvote 0
on the testdata worksheet the only time a date should appear is when a Y is placed in any relevant cell in the H column which triggers and cell in B column. All other times there is no date stamp displayed within the testdata worksheet.


Still on the testdata worksheet. When a Y is placed in any applicable cell (G2-G200) in col G this triggers a date stamp on the results worksheet.

Col B (b2-b200).
I am sorry, I find this very confusing. The comments you made above seem to contradict each other.

Are you saying that the date stamp in column B on the "results" tab are triggered by a "Y" being entered into column G OR column H on your "testdata" sheet?
Which is it?
 
Upvote 0
I am sorry, I find this very confusing. The comments you made above seem to contradict each other.

Are you saying that the date stamp in column B on the "results" tab are triggered by a "Y" being entered into column G OR column H on your "testdata" sheet?
Which is it?

Hi..
no the date stamp on the testdata (col B - triggered by col H ) is completely different to the datestamp on the results worksheet which is triggered by col G .

so if I can perhaps take what should happen


I will start with TestData:

Putting a Y in col H (cells H2-H200) puts a datestamp in Col B (range b2-b200) this has NO impact on the results worksheet.

Results/Testdata worksheets

when a Y is placed into col G (range G2-G200) on the testdata worksheet, this triggers a datestamp on the results worksheet Col B (range b2-b200) this is the only time that both testdata & results worksheets interact.


I hope I have simplified this for you ?
 
Upvote 0
OK, so you really have two different date stamps at work here (this request seems to be getting more and more complex with every post - much more complicated than your initial post implied!).

I added another block to the code to handle the second timestamp:
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
     

'   ********** CODE BLOCK 3 **********
    Dim rng3 As Range
    Dim cell3 As Range
    
'   Check to see if any cells updated in range H2:H200 on "testdata" sheet
    Set rng3 = Intersect(Target, Range("H2:H200"))
    
'   Loop through updated cells
    If Not rng3 Is Nothing Then
'       Unprotect sheet
        ActiveSheet.Unprotect
        For Each cell3 In rng3
            Select Case UCase(cell3)
'               Add date stamp to column B if "Y" added to column H
                Case "Y"
                    Cells(cell3.Row, "B") = Now()
'               Clear date stamp from column B if column H changed to blank
                Case ""
                    Cells(cell3.Row, "B").ClearContents
            End Select
        Next cell3
'       Reprotect sheet
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True
    End If
    
    Application.EnableEvents = True
    

End Sub
I simplified your other code a little, but it shouldn't have any impact on how it runs (other than being a little faster):
Code:
Sub WipeTheSlate()
'
' WipeTheSlate Macro
'
    ActiveSheet.Unprotect
    

If MsgBox("This will clear all data in Columns A-B & E-I. Press Yes to delete or No to stop.", vbYesNo + vbQuestion) = vbNo Then Exit Sub
    
    Range("A2:A200").ClearContents
    Range("B2:B200").ClearContents
    Range("E2:I200").ClearContents
    Range("A2").Select
    
   ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True
        
    
End Sub
If I use your Macro to Clear the data, if I then enter a "Y" into E2, and then into F2, I get the warning message. So everything seems to be working properly, as far as I can see.
 
Upvote 0
good god... you are FAB.
absolutly smashed it...

just wee, wee request/tweak

on the resultsdata sheet.. is it possible to retain the datestamp when the relevant trigger is removed from testdata sheet? This would be great for the managemant statistics daily/weekly report.

KR
Trevor3007:cool:
 
Upvote 0
Do you mean that you want the timestamp on the "results" sheet to remain when the "Y" is removed from column G on the "testdata" sheet?
If so, then just remove this section from Block 2:
Code:
'               Clear date stamp from column B on "results" if column G changed to blank
                Case ""
                    Sheets("results").Cells(cell1.Row, "B").ClearContents
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,571
Members
449,173
Latest member
Kon123

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