【问题标题】:How to get the difference in number of days in excel VBA?如何在excel VBA中获得天数的差异?
【发布时间】:2011-03-18 09:43:35
【问题描述】:

我目前正在使用 Excel VBA 进行图书馆系统项目,我需要一个模块来检查逾期图书,并计算对图书逾期的用户施加的罚款。

这是我拥有的代码,它显然不起作用。

Dim fine, count, count2 As Integer
Dim currentdate, borroweddate, difference As String


'this is the amount of fine imposed per day (50 cents)
fine = 1 / 2

'gets number of borrowing records
count = Sheets("borrowing records").Range("K1").Value

'count2 is just a counter for the cells
count2 = 2

'gets the current date
currentdate = Now()


While (count2 < count + 2)
    If (Sheets("borrowing records").Cells(count2, 8).Value = "ON LOAN") Then
        difference = currentdate - Sheets("borrowing records").Cells(count2, 4).Value

        If (difference > 20) Then
            'prints the overdue record
            Sheets("display").Cells(count2, 1).Value = Sheets("borrowing records").Cells(count2, 1).Value
            Sheets("display").Cells(count2, 2).Value = Sheets("borrowing records").Cells(count2, 2).Value
            Sheets("display").Cells(count2, 3).Value = Sheets("borrowing records").Cells(count2, 3).Value
            Sheets("display").Cells(count2, 4).Value = Sheets("borrowing records").Cells(count2, 4).Value
            Sheets("display").Cells(count2, 5).Value = difference * fine
        End If

    Else
        count2 = count2 + 1

    End If
Wend

Sheets("display").Select

当我尝试运行类似的代码来测试差异值时,我得到了带有很长小数的随机数。我不知道为什么。我得到的结果甚至与天数的实际差异都不接近。例如,如果我测试 3 月 20 日和 3 月 1 日之间的天数差异,我会得到类似 4.35648920486E32E 的结果,是的,结果中有一个字母“E”。

我的初衷是让编码搜索工作表(“借阅记录”),程序存储借阅书籍的记录。

程序会比较这本书的借阅日期和当前日期,看看是否超过了 20 天。

如果是这样,它将通过将日期差乘以每天的罚款金额(在本例中为 50 美分)来计算罚款。然后程序应在另一个工作表(“显示”)中打印过期记录以及罚款。

我知道该程序无法运行,可能是因为日期格式或类似的原因。但是,我不知道如何解决它。

我的项目将于周一到期,我只是希望阅读本文的人能帮助我完成它。谢谢!

【问题讨论】:

  • "它显然不起作用。" - 如何?为什么?
  • 当我尝试运行类似的代码来测试差异值时,我得到了带有很长小数的随机数。我不知道为什么。
  • 这很可能是一个未格式化的日期。 Excel 将日期/时间表示为十进制数字:现在它是 -> 40602.41139 作为 excel 数字格式的日期/时间
  • 感谢您的告知,但是我该如何格式化日期???我需要一个可以从另一个日期中扣除的日期格式。
  • 要么将列格式化为日期列,就像您通常通过格式化单元格所做的那样。或通过NumberFormat 我相信(这是Cell 属性)在代码中设置格式

标签: excel search vba date sequential


【解决方案1】:

因为我们的评论链太多了。我想我会开始一个新的答案:

对于您的日期差异,请使用 DateDiff 函数,如下所示:

difference = DateDiff("d", currentdate, Sheets("borrowing records").Cells(count2, 4).Value, vbMonday)

其中"d" 表示以天数提供差异,vbMonday 告诉系统我们的一周从星期一开始(我认为它默认为星期日 - 现在怀疑与您相关,但请记住未来用途)

这将为您提供一个简单的天数答案,这就是您所追求的。将它与您的浮动罚款值相乘,我认为这就是您解决当前问题所需要的全部。无需进行日期格式化,因为结果只是一个普通数字。

【讨论】:

  • 非常感谢!这正是我需要的:D
  • 恩,请将 Maverik 的帖子设置为已接受的答案(通过单击他帖子旁边的复选标记)。这对他(他为此获得积分)对社区帮助者(他们会知道问题已经回答)以及对有类似问题的访客有好处。
【解决方案2】:
Dim fine, count, count2 As Integer
Dim currentdate, borroweddate, difference As String

'this is the amount of fine imposed per day (50 cents)
fine = 1 / 2   ' integers don't do floating point!

您查看过fine 的值吗?我认为您可能会发现您对逾期借款人的收费不足;-)

这里发生了什么?

difference = currentdate - Sheets("borrowing records").Cells(count2, 4).Value

尝试添加一行以查看您是否得到了预期的答案,例如

difference = currentdate - Sheets("borrowing records").Cells(count2, 4).Value
Debug.Print currentdate, Sheets("borrowing records").Cells(count2, 4).Value, difference

【讨论】:

  • 嘿,对于“difference = currentdate - sheet("borrowing records").cells(count2,4).value”这一行,我试图计算天数的差异,或者罚款的天数。 sheet("borrowing records").cells(count2,4).value 是存储借书日期的单元格。此日期源自 VBA 函数 Now()。因此,其他 cmets 提到的格式可能存在一些问题。至于罚款,我不挣。所以谁在乎:D
  • 我在维基百科上快速阅读了你提到的浮点数,所以我必须对整数使用浮点表示?我是一个新程序员,我真的不知道该怎么做,如果可以的话请帮助我:D
  • Dim fine As Single 会给你一个浮点数(或者As Double 如果你需要更高的精度)。另一个提示:在 vba 中使用 Val 函数,该函数将从字符串中获取正确的项目(例如,如果您在 String 上使用 Val,则表示为文本的 Integer 将获取 Integer
猜你喜欢
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2021-04-30
相关资源
最近更新 更多