• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

If a timestamp is between 2 other timestamps, do something

rickyspires

New Member
Hello.

Please can someone help me with this as it is driving me around the bend. I have tried for weeks.

What I would like to do is...
For example...

If timestamp "25/10/2023 05:06:07" if greater than "25/10/2023 02:03:04" and less than "26/10/2023 12:23:34" do something.


Please can someone help.

Thank you
 
this formula =IF(AND(A1>B1,A1<C1),"Do Something","Do Nothing") with this data in A1:C1

10/25/2023 5:06​
10/25/2023 2:03​
10/26/2023 12:23​
 
this formula =IF(AND(A1>B1,A1<C1),"Do Something","Do Nothing") with this data in A1:C1

10/25/2023 5:06​
10/25/2023 2:03​
10/26/2023 12:23​
Thank you for your answer, but it does not work for me as macro. I need to use a macro and not an excel formular because I am getting the images date created in the code.

I tried this..

imageTimeStamp = "25/10/2023 05:06:07" 'A1
lowerTimeStamp = "25/10/2023 02:03:04" 'B1
upperTimeStamp = "26/10/2023 12:23:34" 'C1

results = IF(AND(imageTimeStamp>lowerTimeStamp,imageTimeStamp<upperTimeStamp),"Do Something","Do Nothing")
MsgBox "results" & Results

but it does not like the IF(AND at the start.
 
Code:
Option Explicit


Sub Ricky()
    Dim i As Long, lr As Long, s As Worksheet
    Set s = ActiveSheet
    lr = s.Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        If s.Range("A" & i).Value > s.Range("B" & i).Value And s.Range("A" & i).Value < s.Range("C" & i).Value Then
            s.Range("D" & i) = "Do Something"
        Else: s.Range("D" & i) = "Do Nothing"
        End If
    Next i
    MsgBox "Action Completed"
End Sub
 
Alternatively
Code:
Sub DoSomethingOrNot()
    Dim imageTimeStamp As Date
    Dim lowerTimeStamp As Date
    Dim upperTimeStamp As Date

    imageTimeStamp = DateStrToDate("25/10/2023 05:06:07")    'A1
    lowerTimeStamp = DateStrToDate("25/10/2023 02:03:04")    'B1
    upperTimeStamp = DateStrToDate("26/10/2023 12:23:34")    'C1

    'If timestamp "25/10/2023 05:06:07" if greater than "25/10/2023 02:03:04" and less than "26/10/2023 12:23:34" do something.
    If (imageTimeStamp > lowerTimeStamp) And (imageTimeStamp < upperTimeStamp) Then
        '
        ' Do Something
        '
    Else
        '
        ' Or Not
        '
    End If
End Sub

Function DateStrToDate(DateTimeStr As String) As Date
    Dim DArr, TArr
    Dim TimeStamp As Date
    
    DArr = Split(Left(DateTimeStr, 10), "/")
    TArr = Split(Right(DateTimeStr, 8), ":")

    TimeStamp = DateSerial(DArr(2), DArr(1), DArr(0)) + TimeSerial(TArr(0), TArr(1), TArr(2))
    DateStrToDate = TimeStamp
End Function
 
Back
Top