简体   繁体   English

Excel VBA 减去两个日期以计算整个列中的天数

[英]Excel VBA Substract two dates to count days in entire columns

I am trying without a positive result a function that calculates difference in days between days in Column "B", B2:B range and today date that is in cell "K2" and results are being transferred into in column 7, range: G2:G.我正在尝试没有正面结果的 function 计算列“B”、B2:B 范围和单元格“K2”中的今天日期之间的天数差异,结果正在转移到第 7 列,范围:G2: G。 So far I am getting more and more excited about VBA coding, but as a person that needs more exp in this I would like to ask for some help about solving this issue.到目前为止,我对 VBA 编码越来越兴奋,但作为一个需要更多经验的人,我想寻求一些帮助来解决这个问题。

Sub DateSub()
    Dim strInput As String, strOutput As String
    Dim start_date As Date
    Dim end_date As Date
    Dim diff As Integer
    
   Dim LastRowcheck As Long, n1 As Long
    Sheets("T1").Range("K2") = Format(Date, "DD-MM-YY") 'today
    end_date = Sheets("T1").Cells(2, 11).Value
    LastRowcheck = Sheets("T1").Range("B" & Rows.Count).End(xlUp).Row

  For n1 = 1 To LastRowcheck
    With Worksheets("T1").Cells(n1, 2)
        start_date = Sheets("T1").Cells(n1, 2).Value
        diff = DateDiff("D", start_date, end_date) ' HERE IS ISSUE
        Cells(nl + 1, 2) = diff
        Sheets("T1").Cells(n1 + 1, 7).Select
        Selection.NumberFormat = "dd-mm-yy"
        
   End With
  Next n1
    Application.DisplayAlerts = False

End Sub

Not sure if it's the cause of your overflow, however be aware of the following.不确定这是否是导致溢出的原因,但请注意以下事项。 Especially working with dates, you can easily get an overflow error if using the Integer data type.特别是使用日期,如果使用Integer数据类型,您很容易得到溢出错误。

Per the documentation for VBA Data type's ;根据VBA 数据类型的文档;

Integer 2 bytes -32,768 to 32,767

Change your data type for diff to Long - Dim diff As Long .diff的数据类型更改为Long - Dim diff As Long

Long (Long integer) 4 bytes -2,147,483,648 to 2,147,483,647

Insert the date value , not a formatted string:插入日期,而不是格式化字符串:

Sheets("T1").Range("K2") = Date 'today

Apply the date format to K2.将日期格式应用于 K2。

Sub DateSub()
    Dim strInput As String, strOutput As String
    Dim start_date As Date
    Dim end_date As Date
    Dim diff As Integer
    Dim LastRowcheck As Long
    Dim n1 As Long
    Sheets("T1").Range("K2") = Format(Date, "DD-MM-YYYY") 'today
    end_date = Sheets("T1").Cells(2, 11).Value
    LastRowcheck = Sheets("T1").Range("B" & Rows.Count).End(xlUp).Row
    Sheets("T1").Activate
    For n1 = 2 To LastRowcheck
        start_date = Sheets("T1").Cells(n1, 2).Value
        diff = DateDiff("D", start_date, end_date) 
        Cells(n1, 3) = diff
        Cells(n1 + 1, 7).Select
        Selection.NumberFormat = "dd-mm-yy"
    Next n1
    Application.DisplayAlerts = False
End Sub

Above codes works fine and is tested.上面的代码工作正常并经过测试。 Few things changed and corrected.很少有事情改变和纠正。

  1. Cells(nl + 1, 2) = diff will overwrite the next row in same column with diff, this should be shifted to next column Cells(nl + 1, 2) = diff将用 diff 覆盖同一列中的下一行,这应该转移到下一列

  2. Format(Date, "DD-MM-YY") changed to YYYY to avoid confusion of 19th or 20th century Format(Date, "DD-MM-YY")更改为 YYYY 以避免混淆 19 世纪或 20 世纪

  3. with is not required with不是必需的

  4. I have changed the for loop to start from row 2, you can change it back to 1, but make sure that its not empty otherwise you will get overflow error我已将 for 循环更改为从第 2 行开始,您可以将其更改回 1,但请确保其不为空,否则会出现溢出错误

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

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