【发布时间】: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