VBA Formula in table

DudethatsSweet

New Member
Joined
Jul 5, 2018
Messages
3
Hi!

Im trying to get this formula to work. It calculates the total hours from start time to end time and converts it to numbers instead of time.
When executed it gives a runtime 1004 error. When i copy the formula by hand it will work just fine. Can someone please help me out?
Below the VBA code. Thanks in advance!

Sub CalculateTotalHours()
Dim tbl As ListObject
Dim sFormula As String


Set tbl = Sheets("Data").ListObjects("DataTable")

sFormula = "=IF([@[End Time]]<[@[Start Time]];(([@[End Time]]-[@[Start Time]]+24)*24);([@[End Time]]-[@[Start Time]])*24)"

tbl.ListColumns(").DataBodyRange.Cells(1).Formula = sFormula
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Sorry, im new and dont know how to edit the post. Made a typo at the last line of the code

tbl.ListColumns("Total Hours").DataBodyRange.Cells(1).Formula = sFormula
 
Upvote 0
Try replacing the semi colons (;) with commas (,).
Code:
Sub CalculateTotalHours()
Dim tbl As ListObject
Dim sFormula As String

    Set tbl = Sheets("Data").ListObjects("DataTable")

    sFormula = "=IF([@[End Time]]<[@[Start Time]],(([@[End Time]]-[@[Start Time]]+24)*24),([@[End Time]]-[@[Start Time]])*24)" 

    tbl.ListColumns("Total Hours).DataBodyRange.Cells(1).Formula = sFormula 

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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