VBA to add a new row to a specified table and insert a formula

RSAmon

New Member
Joined
Mar 8, 2018
Messages
2
Hello

This is my first post so I'm hoping someone can help me (and that I've stuck to the rules!).

I'm trying to set up a small database within Excel for work. In this, I have a table called OFI_Data which will hold all the records. Within the OFI_Data table, there is a column called Target Date which will initially calculate the Target Date as:

=IF([Raised Date]="","TBC",[Raised Date]+30)

However, if an extension is required, the Target Date will be manually adjusted later on so I can't just rely on the autofill function of the table.

I also have a Create New Record button which has been assigned the following (very basic) Macro:

Code:
Sub Create_New_Record()
ActiveSheet.ListObjects("OFI_Data").ListRows.Add AlwaysInsert:=True
End Sub

This code works perfectly to add a new row to the bottom of the table, providing I am already in the table. If a cell outside the table is active though, the button doesn't work.

My question is, please could someone help me with the VBA code to:

  • Insert a new row at the bottom of the OFI_Data table regardless of what cell is active on the worksheet (which is called Database)
  • Insert the default 30 day Target Date formula into the Target Date column when the new row is added (even though that formula might not be used in the cell directly above it)

I hope this is clear enough for someone to offer some help. I have tried looking online but most answers either involve using the autofill function, or copying the formula from the row above which I can't use in this case.

If you need any more information, please let me know. Thank you in advance!
R
 

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.
alter the names to fit your sheet.
and the relative position for the formula....
here: RC[-2] says the Raised Date ​cell is 2 columns to the left.

Code:
Sub Create_New_Record()
sheets("database").activate
range("A2").select
ActiveSheet.ListObjects("OFI_Data").ListRows.Add AlwaysInsert:=True
r = activecell.row
range("A" & r).FormulaR1C1 = "=IF(RC[-2]="""",""TBC"",RC[-2]+30)"
End Sub
 
Upvote 0
Hello - sorry for the delay, finally managed to get time to try this out in the database and it worked beautifully so thank you very much. The only thing I did notice was it also put this formula into the A2 cell on the worksheet, which isn't part of the table. I'm not sure how to stop that from happening, but it's not a big deal (I've just made the text colour white on that cell so no-one will ever know!) Thank you again!
 
Upvote 0

Forum statistics

Threads
1,223,748
Messages
6,174,264
Members
452,553
Latest member
red83

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