MartinCosk
New Member
- Joined
- Aug 31, 2024
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
Hello,
My worksheet is an event log, which allows the user to record events and put a permanent date/time stamp on the entry. I am using a slightly modified widely available code to insert a new empty row below the last entry. The time stamp occurs when the user enters the data, not when a new row (new event) is selected. When a new event is selected, the code copies the last row, and then pastes the formatting plus the formulas in cells B and C to ensure the time stamp takes place.
This is working fine but on occasion there may be some catch up on an event that occurred earlier and a manual entry on the time is needed (cell B), which of course deletes the formula. When a new event is selected, the new cell B contains the manually entered time, not the formula required to stamp the time. I understand how to code to put a specific formula into a specific cell but adding this to a new row with increments to match the row number is causing me a problem.
So, I am after some help to insert an incremental formula into a specific cell in a new row each time the new row is created. The next step will be to have the data sorted in time order, either on a new row or enter but that is probably next weekend’s task….
The worksheet layout is
Cell B = Time
Cell C = Date
Cell D = event text
Formula for time/date stamp is =IF(D12<>"",IF(B12<>"",B12,NOW()),""). (I have code in the workbook page to enable iterative calculations on opening)
Many thanks for any help and welcome any comments if there is a need to make the code more efficient.
My worksheet is an event log, which allows the user to record events and put a permanent date/time stamp on the entry. I am using a slightly modified widely available code to insert a new empty row below the last entry. The time stamp occurs when the user enters the data, not when a new row (new event) is selected. When a new event is selected, the code copies the last row, and then pastes the formatting plus the formulas in cells B and C to ensure the time stamp takes place.
This is working fine but on occasion there may be some catch up on an event that occurred earlier and a manual entry on the time is needed (cell B), which of course deletes the formula. When a new event is selected, the new cell B contains the manually entered time, not the formula required to stamp the time. I understand how to code to put a specific formula into a specific cell but adding this to a new row with increments to match the row number is causing me a problem.
So, I am after some help to insert an incremental formula into a specific cell in a new row each time the new row is created. The next step will be to have the data sorted in time order, either on a new row or enter but that is probably next weekend’s task….
The worksheet layout is
Cell B = Time
Cell C = Date
Cell D = event text
Formula for time/date stamp is =IF(D12<>"",IF(B12<>"",B12,NOW()),""). (I have code in the workbook page to enable iterative calculations on opening)
Many thanks for any help and welcome any comments if there is a need to make the code more efficient.
VBA Code:
Sub Insert_New_Rows()
Dim Lr As Integer
Lr = Range("B" & Rows.Count).End(xlUp).Row 'Searching last row in column B
Rows(Lr + 1).Insert Shift:=xlDown 'Inserting new row
Rows(Lr).Copy 'Copying format of last row
Rows(Lr + 1).PasteSpecial Paste:=xlPasteFormats 'Pasting format to new row
Rows(Lr + 1).PasteSpecial Paste:=xlPasteFormulas ' paste formulas for time / date stamps into new row
Rows(Lr + 1).SpecialCells(xlConstants).ClearContents 'clear contents for the new row
Range("B" & ActiveCell.Row & ":C" & ActiveCell.Row).Copy
Application.CutCopyMode = False 'Deactivating copy mode
End Sub