1

i have a excel sheet that has time values like

A1 00:01:00

A2 00:02:00

I want to get data using a for loop for certain times. i use a conditional formula like below

if Cells(1,1).Value = TimeValue("00:01:00") Then Do Something

I can get the values between 00:01:00 - 00:30:00, but for the values between 00:30:00 - 00:59:00 the formula Cells(#).Value = TimeValue("00:33:00") does not work. the if condition does not return a true value even if the cell content is 00:33:00. What is the problem. I tried to use #12:33:00 AM# etc., it did not work either. What can be the problem? (I formatted the cells as time format).

Community
  • 1
  • 1

1 Answers1

1

You need to first convert to its base type by converting to double.

Then there is the issue of comparing floating point numbers in vba. Plenty of topics out there already I wont flog a dead horse. You can use round or simply convert to a string should do the job.

If CStr(CDbl(Sheet1.Cells(1, 1).Value)) = CStr(CDbl(TimeValue("00:33:00"))) Then

Further reading on floating point errors: Link Link2 Link3

Community
  • 1
  • 1
Reafidy
  • 8,240
  • 5
  • 51
  • 83