我目前正在使用 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 美分)来计算罚款。然后程序应在另一个工作表(“显示”)中打印逾期记录以及所施加的罚款。
我知道该程序无法运行,可能是因为日期的格式或类似的东西。但是,我不知道如何解决它。
我的项目将于周一到期,我只是希望有人阅读此内容能帮助我完成它。谢谢!