DateDiff VBA function not working

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
The below code gives a run-time error 1004 (undefined object) and it is isolating the DateDiff function as the problem. What gives?

Code:
Sub Speed_DoesNotWork
Dim start_time, end_time
start_time = Now()
end_time = Now()
Sheets("SpeedLog").Select
Range("C1").End(xlDown).Offset(1, 0).Value = DateDiff("s", start_time, end_time)
End Sub

The below code works fine:

Code:
Sub Speed_Works
Dim start_time, end_time
start_time = Now()
end_time = Now()
MsgBox "This CVTY Speed Search was completed in" & " " & (DateDiff("s", start_time, end_time)) & " " & "seconds."
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Is there anything in C2? If not, you are probably trying to enter the value in the cell below the last cell in column C... :)
 
Upvote 0
C2 is an empty cell.

What do I need to do fix the problem?

My goal is to fill the next empty cell (row) in Column C each time I run the routine.

"C1" is the header for the column.

Thanks for the help.
 
Upvote 0
Search up rather than down:
Code:
Sub Speed_Test()
Dim start_time, end_time
start_time = Now()
end_time = Now()
Sheets("SpeedLog").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = DateDiff("s", start_time, end_time)
End Sub
 
Upvote 0
Yikes...

Any idea why the value is being replaced each time the search is being done?

C1 = Header
C2 = Value
**Each time I run the test, I would like a new value placed below the filled cell**

Test 1 = C2
Test 2 = C3
Test 3 = C4, etc
 
Upvote 0
Would this code help at all in getting the successive values to be inserted in the empty cell below?:

Code:
Sub InsertCellValues()
Dim LastRow As Integer        ' input below to specify how many rows to check beginning with 1st row
Dim counter As Integer        ' loop counter from 1 to lastrow
Dim InvertedCounter As Integer        ' counter from lastrow down to 1
Dim DeleteCondition As Boolean
With Sheets("SpeedLog")
  LastRow = 100
  For InvertedCounter = LastRow To 1 Step -1
    If .Cells(InvertedCounter, 2) = 0 And .Cells(InvertedCounter, 5) = 0 Then
      .Rows(InvertedCounter).Delete
    End If
  Next InvertedCounter
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

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