Forum Discussion
Posting a date based on an event
Hello Forum,
Does any one know of a way thru a formula to post a date into a cell based on an event happening on other sheet.
I have a sheet that makes a random selection of people from my roster whereby I copy the selection and paste the values into cells on the same sheet in a different column. The thing is however, I have another Sheet in the same workbook that I need to have the selection date, the paste event date, based on the selection of the individual(s), placed/posted/dropped into the appropriate column/cell as applicable.
Can anyone help me out with this?
Carl
7 Replies
- AlikocIron Contributor
Hello Carl,
I think you can fix it by adding the following command. Can you try it and let me know?
Private Sub Worksheet_Change(ByVal Target As Range) Dim targetSheet As Worksheet Dim dateColumn As Range ' Set target sheet where the date should be posted Set targetSheet = ThisWorkbook.Sheets("Sheet2") ' Change "Sheet2" to the actual name ' Define the range where the paste event happens Set dateColumn = Me.Range("B:B") ' Assuming you are pasting in column B on Sheet1 ' Check if the pasted cells are in the specified range If Not Intersect(Target, dateColumn) Is Nothing Then ' Find the first empty cell in the target column on the target sheet Dim emptyRow As Long emptyRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row + 1 ' Adjust "A" as needed ' Post the current date in the target sheet targetSheet.Cells(emptyRow, "A").Value = Date ' Adjust "A" to the desired column for the date End If End Sub
Best Regards,
Ali Koc
Try this:
1: Set Up Your Sheets
- Sheet 1: This sheet contains your roster and the random selection of people.
- Sheet 2: This sheet will display the selection date based on the event happening on Sheet 1.
2: Create a Trigger Event
You need to create a trigger event on Sheet 1 that will update the date on Sheet 2. You can use the IF and VLOOKUP functions to achieve this.
3: Write the Formula
On Sheet 2, in the cell where you want to display the selection date, use the following formula:
excel
=IF(VLOOKUP(A2,Sheet1!A:A,1,FALSE)<> "", TODAY(), "")
4: Copy the Formula
Copy the formula to other cells in the same column on Sheet 2 where you want to display the selection date.
- Carl_61Iron Contributor
Based on the possible solution you provided and my reply to you with all the info on what is going on, would you please help me formulate the solution.
- Carl_61Iron Contributor
Very nice. This may do the trick if I can get on the same page with you. So here is what is going on. I have a sheet called "BO Schedule" using 5 columns. Column "B" is a column labeled "BO BEGIN DATE". This column contains the following formula: =LET(firstday, DATE(B2, 1, 1), firstmonday, firstday+7-WEEKDAY(firstday, 12), weeks, ROUNDUP((EDATE(firstday, 12)-firstmonday)/7, 0), SEQUENCE(weeks, , firstmonday, 7)). This generates a column of dates all of which start on a Monday. Formatted as: Monday, January 6, 2025. Cell "B1" is labeled as "BO YEAR" & cell "B2" is for the year. In this case cell "B2" = 2025. This generates me a column of Monday dates for year 2025 starting with Monday, January 6, 2025 thru Monday, December 29, 2025. 52 Mondays.
Column "C" cell "C3" is labeled as "MALE BO". Starting in cell "C4", This column is where the Randomly selected Male persons are pasted to from column "E". Column "E" has this formula in it:
=LET( n, FILTER(Room_Roster[NAME], (Room_Roster[NAME]>" ")*(Room_Roster[GNDR]=$E$3)*(Room_Roster[POSITION]<>Lists!L9)*(Room_Roster[Occ Status]="O")*(Room_Roster[Duty Completed Date]<=EDATE(TODAY(),-Lists!L7))*(Room_Roster[Room Assign/Check-in Date]<=EDATE(TODAY(), -Lists!L5))), count, ROWS(n), SORTBY(n, RANDARRAY(count))).
Column "E" cell "E3" has an "M" in it. M = Male
Column "D" cell "D3" is labeled as "FEMALE BO". Starting in cell "D4", This column is where the Randomly selected Female persons are pasted to from column "F". Column "F" has this formula in it:
=LET( n, FILTER(Room_Roster[NAME], (Room_Roster[NAME]>" ")*(Room_Roster[GNDR]=$F$3)*(Room_Roster[POSITION]<>Lists!L9)*(Room_Roster[Occ Status]="O")*(Room_Roster[Duty Completed Date]<=EDATE(TODAY(),Lists!L7))*(Room_Roster[Room Assign/Check-in Date]<=EDATE(TODAY(), -Lists!L5))), count, ROWS(n), SORTBY(n, RANDARRAY(count))).
Column "F" cell "F3" has an "F" in it. F = Female
The desire here is to post the date this paste takes place, IE: the "Selected Date", into the "Selected Date" column, column "AE" starting in cell "AE3" on the ALL TAB sheet, applicable to the matching individual that was selected. The ALL TAB is named "Room_Roster" and the [ ] depict the column labels accordingly.
The formulas above are making selections based on criteria with in the formula and copied and pasted.
On the ALL TAB, within the Room_Roster columns, column "AI" is receiving the "BO Schedule" "Begin Date" using the following formula:
=IF([@NAME]="", "", XLOOKUP([@NAME], IF([@GNDR]="M", 'BO Schedule'!$C$4:$C$108, 'BO Schedule'!$D$4:$D$108), 'BO Schedule'!$B$4:$B$108, ""))
I am just trying to also post the Date into the "Selected Date" column, the date the Begin Date is posted against the individual who was selected.
Lastly, the "Lists" mentioned in the formulas, refers to the "Lists Tab" which has the info needed for "Lists!L9" & "Lists!L7". Currently, "Lists!L7" is "12" and "Lists!L9" is "SG". Currently, "Lists!L5" is "6".
This may be more than you need but I wanted to be clear on what I need when it comes to the posting of the "Selected Date" and the trigger causing the date to be posted.
Thank you so much for your reply and trying to help me out. I hope you can assist me with making this happen.
V/r,
Carl W. Ladd
- Carl_61Iron Contributor
Based on the possible solution you provided and my reply to you with all the info on what is going on, would you please help me formulate the solution.
- NikolinoDEGold Contributor
To achieve this, you can use a combination of formulas like IF and TODAY to post a date based on an event, but the main challenge is that formulas themselves cannot detect events like copying and pasting. However, you can use a VBA macro to automatically update a date when a specific event occurs, such as pasting values into certain cells. Here’s how you can approach this:
Option 1: Using VBA for Event Tracking
1. Open the VBA Editor:
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
2. Create a Worksheet Change Event:
- In the Project Explorer, double-click on the sheet where you want to detect the paste event (e.g., Sheet1).
3. Paste the Following Code:
Vba Code is untested backup your file
Private Sub Worksheet_Change(ByVal Target As Range) Dim watchRange As Range Dim cell As Range ' Set the range to monitor (e.g., cells where you paste values) Set watchRange = Me.Range("B2:B100") ' Adjust as needed ' Check if the change was within the monitored range If Not Intersect(Target, watchRange) Is Nothing Then Application.EnableEvents = False ' Loop through each changed cell in the watch range For Each cell In Target If Not IsEmpty(cell.Value) Then ' Post the current date in another sheet (e.g., Sheet2) Worksheets("Sheet2").Cells(cell.Row, "C").Value = Date ' Adjust column as needed End If Next cell Application.EnableEvents = True End If End Sub
4. Adjust the Code:
- Change watchRange to match the cells in which you paste values.
- Modify Worksheets("Sheet2").Cells(cell.Row, "C") to specify where the date should be posted on the other sheet.
Explanation:
- watchRange: Specifies the range of cells where pasting values triggers the macro.
- Date Posting: The code posts the current date (Date function) into the specified cell on Sheet2 whenever a value is pasted into the monitored range.
- Application.EnableEvents = False: Prevents the macro from triggering itself when updating cells.
Option 2: Formula-Based Workaround (Limited)
Formulas alone cannot detect a paste event, but you can try setting up a helper cell and using conditional logic:
1. Helper Column:
- On Sheet1, set up a helper column that changes based on specific criteria.
2. TODAY() Formula:
- On Sheet2, use an IF formula to post a date if a condition is met:
=IF(Sheet1!B2<>"", TODAY(), "")
- This will show today’s date when a cell in Sheet1 has a value, but the date will update every day. This method is not persistent and only works as a temporary solution.
Limitations:
- VBA Approach: Requires macros to be enabled and might need user permission to run.
- Formula Approach: Not ideal for event detection as it will continuously change the date.
The VBA solution is the most effective way to detect and respond to pasting values in cells and to post a date in another sheet accordingly.
The text and steps were edited with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- Carl_61Iron Contributor
Thank you for providing me a possible solution. My ability to use VBA or Macros has been locked down but do prefer that route if and when possible. This is why I am searching for a formula based solution. I could use VBA on a lot of what I am doing but my company has decided they don't want it enabled. For me, this is sad.
Carl