将日期转换为日终,excel vba

将日期转换为日终,excel vba,excel,vba,Excel,Vba,下面的代码不断增加23:59小时的日期,我需要运行代码4-5次一天,它将添加,直到它改变了初始日期。我只想让它一直持续到那天结束而不加上它 Dim x As Integer Dim test1 As Date Dim schdWS, compWS As Worksheet Const H23M59 As Double = 1 - 60 / 86400 Set schdWS = Sheet1 Set compWS = Sheet11 lastrow = schdWS.Cells(Ro

下面的代码不断增加23:59小时的日期,我需要运行代码4-5次一天,它将添加,直到它改变了初始日期。我只想让它一直持续到那天结束而不加上它

Dim x As Integer
Dim test1 As Date
Dim schdWS, compWS As Worksheet
Const H23M59 As Double = 1 - 60 / 86400

Set schdWS = Sheet1
Set compWS = Sheet11

     lastrow = schdWS.Cells(Rows.Count, 8).End(xlUp).Row

        For x = 20 To lastrow

                If IsDate(schdWS.Cells(x, 3).Value) Then
                       Cells(x, 3).Value = CDate(Int(Cells(x, 3).Value + H23M59))
                End If
Next x

End sub

将循环内的
替换为以下内容

If IsDate(schdWS.Cells(x, 3).Value) Then
    'Get rid of hours, min and secs
    TheDay = Int(schdWS.Cells(x, 3).Value)
    TheEndOfDay = TheDay + H23M59
    'I write the result in col4 for clarity. Replace according to your needs.
    schdWS.Cells(x, 4).Value = Format(TheEndOfDay, "YYYY/MM/DD HH:MM:SS")
End If

无法理解你想要什么。您的代码很清楚:如果C列是日期,请添加常量(几乎为1天)。您希望实现什么?例如,我的c列日期是12/June 00:00,只要运行一次代码,它将是12/June 23:59小时,如果我再次运行,它将是13/June 23:58小时。我需要代码来识别日期,并运行宏4-5次,仍然是12/June 23:59小时。