Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excel VBA中获得天数差异?_Excel_Search_Vba_Date_Sequential - Fatal编程技术网

如何在excel VBA中获得天数差异?

如何在excel VBA中获得天数差异?,excel,search,vba,date,sequential,Excel,Search,Vba,Date,Sequential,我目前正在使用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 borr

我目前正在使用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
Dim fine,count,count2为整数
Dim currentdate、借入日期、差异为字符串
“这是每天的罚款额(50美分)
罚款=1/2
'获取借用记录的数目
计数=张数(“借用记录”).范围(“K1”).值
“count2只是单元格的计数器
count2=2
'获取当前日期
currentdate=Now()
而(count220),则
'打印过期记录
表格(“显示”).单元格(count2,1).值=表格(“借用记录”).单元格(count2,1).值
表格(“显示”).单元格(count2,2).值=表格(“借用记录”).单元格(count2,2).值
表格(“显示”).单元格(计数2,3).值=表格(“借用记录”).单元格(计数2,3).值
表格(“显示”).单元格(count2,4).值=表格(“借用记录”).单元格(count2,4).值
表(“显示”)。单元格(计数2,5)。值=差异*精细
如果结束
其他的
count2=count2+1
如果结束
温德
图纸(“显示”)。选择

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

我的初衷是通过编码在工作表(“借阅记录”)中进行搜索,程序在其中存储借阅书籍的记录

该程序将比较这本书的借阅日期和当前日期,看看20天的期限是否达到

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

我知道这个程序不起作用,可能是因为日期的格式或诸如此类的原因。然而,我不知道如何解决它

我的项目周一就要交了,我只希望有人读到这篇文章能帮我完成。谢谢

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 = DateDiff("d", currentdate, Sheets("borrowing records").Cells(count2, 4).Value, vbMonday)
其中,
“d”
表示以天数表示的差异,并且
vbMonday
告诉系统我们的一周从周一开始(我认为它默认为周日-我现在怀疑与您相关,但请记住,以备将来使用)


这将给你一个简单的天数答案,这就是你所追求的。把它和你的浮动罚款值相乘,我认为这就是你现在需要解决的所有问题。无需进行日期格式化,因为结果将只是一个普通数字。

“而且显然不起作用。”-如何?为什么?当我试着运行一个类似的代码来测试差异的值时,我得到了非常长的小数的随机数。我不知道为什么。那很可能是一个无格式的约会。Excel以十进制数字表示日期/时间:现在它是->40602.41139,以Excel数字格式表示日期/时间谢谢告诉我,但是我如何格式化日期???我需要一个可以从另一个日期中扣除的日期格式。可以像通常一样通过“格式单元格”将列格式化为日期列。或者通过
NumberFormat
设置代码中的格式(这是一个
单元格
属性)嘿,对于“difference=currentdate-sheets(“借款记录”).cells(count2,4).value”行,我试图计算天数的差异,或者罚款的天数。表(“借阅记录”)。单元格(count2,4)。值是存储书籍借阅日期的单元格。此日期由VBA函数Now()派生。因此,其他评论中提到的格式可能存在一些问题。至于罚款,我没有从中获利。所以谁在乎呢:DI在维基百科上快速阅读了你提到的浮点运算,所以我必须用浮点表示整数?我是一个新的程序员,我真的不知道如何做到这一点,请帮助我,如果你可以:D
Dim罚款作为单一
将得到你一个浮点(或
作为双
如果你需要更高的精度)。另一个提示:在vba中使用
Val
函数,它将从字符串中获取适当的项(如表示为文本的
Integer
将获取
Integer
,如果您在
String
上使用
Val
非常感谢!这正是我需要的:DBoon,请将Maverik的帖子设置为接受答案(通过点击帖子旁边的复选标记)。这对他(他为此获得分数)对社区帮手(他们知道问题已经得到回答)以及有类似问题的访客都有好处。