简体   繁体   English

EXCEL VBA:仅从所有日期比较日期和月份

[英]EXCEL VBA: comparing day & month only from all the date

I am trying to compare only day from the below interval: 我正在尝试仅比较以下间隔中的一天:

Sheets("DATA").Range("H:H").NumberFormat = "mm/dd/yyyy hh:mm am/pm" Sheets(“ DATA”)。Range(“ H:H”)。NumberFormat =“ mm / dd / yyyy hh:mm am / pm”

I found myself a solution with this IF function that is included in many other IF's 我发现自己使用了这个IF函数的解决方案,该解决方案包含在许多其他IF中

If Sheets("Data").Range("i" & i).Value <> "N/A" And Sheets("Data").Range("K" & i).Value <> "N/A" Then
    If Day(Sheets("Data").Range("i" & i)) = Day(Sheets("DATA").Range("K" & i)) Then
        Sheets("Data").Range("S" & i).Value = "Equals"
    ElseIf Day(Sheets("Data").Range("i" & i)) > Day(Sheets("DATA").Range("K" & i)) Then
        Sheets("Data").Range("S" & i).Value = "Later"
    Else: Sheets("Data").Range("S" & i).Value = "Earlier"
    End If
    Else: Sheets("Data").Range("S" & i).Value = "N/A"
End If

That seems to do the trick - compare only day from the all date interval and write in column S is it the same day, later, or earlier, or "not available". 这似乎可以解决问题-仅比较所有日期间隔中的某一天,然后在S列中输入是同一天,更晚或更早或“不可用”。

However, this didnot not work if one or another date is in another month (as there are high volumes more than 500k rows of data from 2012-2013) 但是,如果一个日期或另一个日期在另一个月份中,则此方法将无效(因为2012-2013年的大量数据超过50万行)

How can I compare year/month/date from this format "mm/dd/yyyy hh:mm am/pm" without - deleting hours and minutes from the original row (creating new row without hours/minutes is also not an option) 如何通过这种格式“ mm / dd / yyyy hh:mm am / pm”比较年/月/日,而无需-从原始行中删除小时和分钟(也不创建没有小时/分钟的新行)

Thanks! 谢谢!

Compare the values of the cells using the Value2 property. 使用Value2属性比较单元格的值。 The Value2 property returns the unformatted value of the cell. Value2属性返回单元格的未格式化值。 Each integer of a stored date/time value represents one day so I've used the Int() method to truncate the stored value so we are only comparing by date, not minutes or seconds. 存储的日期/时间值的每个整数代表一天,因此我使用了Int()方法来截断存储的值,因此我们仅按日期进行比较,而不是分钟或秒。

If Sheets("Data").Range("i" & i).Value <> "N/A" _
        And Sheets("Data").Range("K" & i).Value <> "N/A" Then

    Select Case Int(Sheets("Data").Range("i" & i).Value2)
        Case Is = Int(Sheets("DATA").Range("K" & i).Value2)
            Sheets("Data").Range("S" & i).Value = "Equals"
        Case Is > Int(Sheets("DATA").Range("K" & i).Value2)
            Sheets("Data").Range("S" & i).Value = "Later"
        Case Else
            Sheets("Data").Range("S" & i).Value = "Earlier"
    End Select

Else
    Sheets("Data").Range("S" & i).Value = "N/A"
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM