Getting a type mismatch error while calculating time difference

Gautham A

Board Regular
Joined
May 25, 2020
Messages
107
Office Version
  1. 2016
Platform
  1. Windows
Hello
I'm getting a type mismatch error while calculating the time difference.
In cell A1, I have the value 19/06/2020 05:40:12
In cell B1, I have the value 19/06/2020 05:45:15,
While calculating the difference between them in VBA, I'm getting type mismatch error
I used the below code
VBA Code:
For i=1 to 10
Cells(i,3)=Cells(i,2)- Cells(i,1)
Next i
I need the output in HH:MM:SS format.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
1594487725320.png


If the data in A1 and B1 really are dates, you should have no problems, but if they are texts, then you must convert them to date.
 
Upvote 0
It is in custom format dd/mm/yyyy hh:mm:ss. So does this mean they are in date format?
 
Upvote 0
It does not matter if the cell has that format, if it is text, it will remain text regardless of the format.
Test on a new sheet, manually capture the values in A1 and B1, and test the macro again.
 
Upvote 0
I manually copied this in another sheet and i used Excel formula. I typed in cell C1=B1-A1. I'm getting the correct output. But in VBA I'm still getting type mismatch.
 
Upvote 0
I manually copied
Do not copy the data, maybe it has some details, capture the entire date and time manually.


Try only row1, maybe the problem is in another row, but for now just try the following:
VBA Code:
Sub test2()
  Dim i As Long
  For i = 1 To 1
    Cells(i, 3) = Cells(i, 2) - Cells(i, 1)
  Next i
End Sub

varios 11jul2020.xlsm
ABC
119/06/2020 05:40:12 a.m.19/06/2020 05:45:15 a.m.0:05:03
Data
 
Upvote 0
This means that you have a character in the original data that is converting that date into text.
At the cell level formulas work in a way different from VBA code, excel automatically resolves some issues that VBA does not.
 
Upvote 0
I can see blank spaces in the front of some cells whereas other cells looks okay. Maybe that is the issue
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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