Date interval calculator

Mindlesh

Board Regular
Joined
Apr 2, 2014
Messages
172
I have a table with two columns: Date, Interval. How can I create a button that, when clicked, will:
  • insert a row at the top of the table
  • set the date that the button was clicked
  • calculate the number of days since the button was last clicked?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Providing your table is named Table1:

Code:
Private Sub CommandButton1_Click()

Dim tb As ListObject


ActiveSheet.ListObjects("Table1").ListRows.Add (1)


Set tb = ActiveSheet.ListObjects(1)
tb.DataBodyRange.Cells(1, tb.ListColumns("Date").Index) = Date


MsgBox (Date - tb.DataBodyRange.Cells(1, tb.ListColumns("Date").Index) & " days since the button was pressed.")


End Sub
 
Upvote 0
Sure, try instead:

Code:
tb.DataBodyRange.Cells(1, tb.ListColumns("Interval").Index) = Date - tb.DataBodyRange.Cells(1, tb.ListColumns("Date").Index
 
Upvote 0
Interval is always "0"; if the last Date is 12/21, and the next Date is 12/23, then Interval should be "2".

When I add the macro to an existing spreadsheet (and update the table name) I get a "subscript out of range" error (9) here:
Code:
tb.DataBodyRange.Cells(1, tb.ListColumns("Date").Index) = Date
 
Upvote 0
Yeah, that's me working on no caffeine.

Go for:
Code:
tb.DataBodyRange.Cells(1, tb.ListColumns("Interval").Index) = tb.DataBodyRange.Cells(2, tb.ListColumns("Date").Index - tb.DataBodyRange.Cells(1, tb.ListColumns("Date").Index

As for the error, do you have any other workbooks open at the same time?
 
Upvote 0
I added a couple of missing brackets, and reversed the Date order:
Code:
tb.DataBodyRange.Cells(1, tb.ListColumns("Interval").Index) = tb.DataBodyRange.Cells(1, tb.ListColumns("Date").Index) - tb.DataBodyRange.Cells(2, tb.ListColumns("Date").Index)

I only have the one workbook open.
 
Upvote 0
I got it working by specifying the table name here, too:
Code:
Set tb = ActiveSheet.ListObjects("Table1")

Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,213,515
Messages
6,114,080
Members
448,548
Latest member
harryls

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