VBA Date/time problem

Krb00

New Member
Joined
Jan 21, 2023
Messages
8
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi,

I have two columns: a date/time column A (dd/mm/yyyy hh:nn:ss) and a values column D.
I want to take a specific value in the column D and the corresponding date/time in column A.
After subtracting a time in minutes from that date/time, I want to check if exist a cell containing that resulting date/time in the
first column in order to get its row number.
The code seems to work fine only for some "mins" values,
e.g. mins=19 works,
mins=20 doesn't work.

Hope someone can help me with this.
Thank you

VBA Code:
Sub calc()
Dim rng As Range
Dim ran As Range
Dim maxT As Double
Dim line As Double
Dim roundown As Double
Set rng = Sheets(5).Range("D1:D1000")
maxT = Application.WorksheetFunction.Max(rng)
line = rng.Find(maxT, , xlValues).Row
roundown = Application.WorksheetFunction.RoundDown(maxT, 0)

Dim fin As Integer
Set ran = Sheets(5).Range("D" & line, "D" & line + 100)
For Each cell In ran
If cell.Value < roundown Then
fin = cell.Row - 1
Sheets(5).Range("A" & fin).EntireRow.Interior.ColorIndex = 6
Exit For
End If
Next

Dim dt1 As Date
Dim dt2 As Date

Dim mins As Integer
mins = Sheets(1).Range("E6").Value
dt1 = CDate(Sheets(5).Range("A" & fin))
dt2 = DateAdd("n", -mins, dt1)


With Worksheets(5)
        Dim intv As Range
        Set intv = .Range(.Cells(2, 1), .Cells(2, 1).End(xlDown))
        For Each cell In intv
        If cell = dt2 Then
        MsgBox cell 
        Exit For
        End If
        Next
End With


End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try using Round; that is, instead then using If cell = dt2 Then, use:
VBA Code:
If Round(cell, 6) = Round(dt2, 6) Then
 
Upvote 0
Solution

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

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