Inserting new row based on cell value, and copying select data to the new row

jibuchho

New Member
Joined
Dec 14, 2023
Messages
4
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a file for ongoing preventative maintenance. When a request is complete, they will fill in the completed date and the next date will calculate automatically. Then I want to create a new entry for the Next Date as a new row at the bottom of the table. I want to use a checkbox in the Create Next Entry column but for now will just use TRUE. That should be the trigger to create the entry. It won't simply be copying the existing row however as the "Next Scheduled Date" will need to be replace with "Next Date" and the completed date would again be empty. There are also a few other columns of hidden data for the sake of this example but those would need to copy as well - see below

Column Headings
Job | Next Scheduled Date | Equipment | Location | Schedule | Vendor/Employee | Progress | Completed Date | Next Date | Create Next Entry
 

Attachments

  • Screenshot 2023-12-13 232606.png
    Screenshot 2023-12-13 232606.png
    67.8 KB · Views: 22

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
If you would be very helpful if you could post your example in a manner which allows us to copy/paste it, so we do not need to recreate it (cannot copy/paste images and get them to work like your sheet). MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Also, is all your data actually stored in a Data table in Excel, or is it just a list?
It gets a bit tricker if it is a table, because you actually then need to insert new rows into your table before you can add new data.
 
Upvote 0
Assuming it is a table, the following VBA code should do what you want, but it needs to be placed in the correct module.
To ensure that, follow the steps below:

1. Go to the sheet you want to apply this to
2. Right-click on the sheet tab name at the bottom of the screen
3. Select "View Code"
4. Paste the following VBA code in the VB Editor window that pops up
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim tblName As String
    Dim lr As Long

'   Exit if multiple cells updated at once
    If Target.CountLarge > 1 Then Exit Sub

'   See if value of TRUE added to column J after row 1
    If (Target.Column <> 10) Or (Target.Row = 1) Or (Target.Value <> True) Then Exit Sub

    Application.EnableEvents = False

'   Get last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Get table name of table starting in cell A1
    tblName = Range("A1").ListObject.Name

'   Add new row to bottom of table
    Set ws = ActiveSheet
    Set tbl = ws.ListObjects(tblName)
    tbl.ListRows.Add
    
'   Copy value from column A to new row
    Cells(lr + 1, "A").Value = Cells(Target.Row, "A").Value

'   Copy Next Date to Next Scheduled Date
    Cells(lr + 1, "B").Value = Cells(Target.Row, "I").Value
    
    Application.EnableEvents = True

End Sub

5. Save & close the VB Editor

Now, as you enter TRUE in column J, it will automatically create your new row and copy the data down.
If you have other columns you want to copy, just continue on with the two "Copy" sections of the code. The code should look the same, except for the column references.
 
Upvote 0
Thank you - this is my first time posting. I appreciate your help. The table is named PM. I imported the code and added a couple more copy cells - see code changes below in blue. It's not quite working - no new row is created so nothing is copied. I am not getting an error or debugger. Any suggestions?


2024 Work Orders.xlsm
ABCDEFGHIJ
4Preventative Maintenance
5JobNext Scheduled DateEquipmentLocationScheduleVendor/EmployeeProgressCompleted DateNext DateCreate Next Entry
65 YEAR CALIBRATION3/1/2026Heat/C02 Vent5 years?? 
7ANNUAL FIRE PUMP CISTERN TREAT6/1/2024Fire Pump CisternAnnually??? 
8SERVICE SOFTENERS 1 & 212/1/2024Water SoftenersBi-MonthlyCulligan 
9HVAC START UP4/1/2024HVAC SystemAnnuallyDroegkamp 
10BOILER START UP9/1/2024Boiler 1 & 2AnnuallyDroegkamp 
11HVAC SHUTDOWN9/1/2024HVAC SystemAnnuallyDroegkamp 
123 YEAR BOILER FLUSH & FILL1/1/2026Boiler 1 & 23 yearsDroegkamp
135 YEAR TEST AND CALIBRATE3/1/2027Carbon Monoxide Sensor5 yearsDroegkamp 
14DRYER VENT CLEANING3/1/2024Dryer Vents2 yearsDryer Vent Wizards
PM
Cell Formulas
RangeFormula
I6:I11,I13I6=IF([@[Completed Date]]="","",XLOOKUP([@Schedule],ScheduleDays[Schedule],ScheduleDays[Days])+[@[Completed Date]])
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B6:B50Dates Occurringlast monthtextNO
B6:B50Dates Occurringthis monthtextNO
Cells with Data Validation
CellAllowCriteria
E6:E51List=Lists!$AA$3:$AA$12
F6:F51List=Lists!$W$3:$W$14
G6:G51List=Lists!$U$3:$U$4
C6:C14List=Lists!$Y$3:$Y$27




Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim tbl As ListObject
Dim tblName As String
Dim lr As Long

' Exit if multiple cells updated at once
If Target.CountLarge > 1 Then Exit Sub

' See if value of TRUE added to column J after row 1
If (Target.Column <> 10) Or (Target.Row = 1) Or (Target.Value <> True) Then Exit Sub

Application.EnableEvents = False

' Get last row in column A with data
lr = Cells(Rows.Count, "A").End(xlUp).Row

' Get table name of table starting in cell A5
tblName = Range("A5").ListObject.Name

' Add new row to bottom of table
Set ws = ActiveSheet
Set tbl = ws.ListObjects(tblName)
tbl.ListRows.Add

' Copy value from column A to new row
Cells(lr + 1, "A").Value = Cells(Target.Row, "A").Value

' Copy value from column C to new row
Cells(lr + 1, "C").Value = Cells(Target.Row, "C").Value


' Copy Next Date to Next Scheduled Date
Cells(lr + 1, "B").Value = Cells(Target.Row, "I").Value

Application.EnableEvents = True

End Sub
 
Upvote 0
OK, let's add in a few message boxes for debugging purposes:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim tbl As ListObject
Dim tblName As String
Dim lr As Long

MsgBox "Code is running!"

' Exit if multiple cells updated at once
If Target.CountLarge > 1 Then Exit Sub

' See if value of TRUE added to column J after row 1
If (Target.Column <> 10) Or (Target.Row = 1) Or (Target.Value <> True) Then Exit Sub

Application.EnableEvents = False

' Get last row in column A with data
lr = Cells(Rows.Count, "A").End(xlUp).Row
MsgBox "Last row is " & lr, vbOKOnly

' Get table name of table starting in cell A5
tblName = Range("A5").ListObject.Name

' Add new row to bottom of table
Set ws = ActiveSheet
Set tbl = ws.ListObjects(tblName)
tbl.ListRows.Add

' Copy value from column A to new row
Cells(lr + 1, "A").Value = Cells(Target.Row, "A").Value

' Copy value from column C to new row
Cells(lr + 1, "C").Value = Cells(Target.Row, "C").Value

' Copy Next Date to Next Scheduled Date
Cells(lr + 1, "B").Value = Cells(Target.Row, "I").Value

Application.EnableEvents = True

End Sub
Whenever you update a value, you should see a message saying "Code is running".
And then when you update a TRUE in column J, you should also see another message box telling you the number of the last row.
Does that last row number look correct? It could be problematic if you have something below your table or blank rows at the bottom of your table.
 
Upvote 0
Solution
That is fantastic - problem solved. Thank you!!!
So what was the issue?
All I did was add a few Message Boxes to aid in debugging. I didn't change any code that would change how it is running.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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