键入不匹配错误,将上一个单元格的值加10秒

时间:2019-01-14 16:05:05

标签: excel vba

我想将先前单元的时间值+ 10秒写入单元。

经过大量的谷歌搜索,我尝试了几种方法,但是下面是我刚开始的工作,我想了解的是为什么这种方法不起作用-在我看来,这是合乎逻辑的。

单元格数据采用特殊格式DD:MM:YYYY HH:MM:SS-这可能无法正常工作,但是,如果我手动将+ (10 / (3600 * 24))添加到单元格中,则会成功添加10秒。

日期作为自定义存储,并显示为24/09/2018 08:41:09

Public Sub Add_Row()
    Dim Row As Variant
    Dim LR As Long
    Dim x As Integer
    Dim y As Integer

    LR = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row 'Counts number of rows
    x = 1
    Row = 1

    Do Until x = LR
        If Cells(Row, 2).Value <= 1 Then 'If the value of the cell is less than or equal to one do nothing and increment
            Row = Row + 1
            x = x + 1
        Else
            y = Cells(Row, 2).Value - 1 'Need a variable for the number of rows we require based on how many missed points we have

            For k = 1 To y
                ActiveSheet.Rows(Row).Insert Shift:=xlDown
                Cells(Row, 1).Value = Cells(Row - 1, 1).Value + (10 / (3600 * 24))
            Next

            Row = Row + y + 1
            x = x + 1
        End If
    Loop
End Sub

2 个答案:

答案 0 :(得分:0)

在VBA中将3600乘以3600将产生溢出错误,因为16位整数(两个输入数字均为整数时结果的默认类型)的最大值是32767。您可以使用“#”它将告诉VBA您要将结果视为两倍,如下所示:

Cells(Row, 1).Value = Cells(Row - 1, 1).Value + (10 / (3600# * 24))  

或者您可以使用代表10秒的“#12:00:10 AM#”,而不是像这样尝试计算它:

Cells(Row, 1).Value = Cells(Row - 1, 1).Value + #12:00:10 AM#

希望这会有所帮助。

答案 1 :(得分:-1)

一种简单的方法,可以将格式化后的值增加10秒:

Sub AddTenSeconds()
    Dim s As String, d As Date

    With ActiveCell
        s = Replace(.Text, " ", ":")
        arr = Split(s, ":")
        d = DateSerial(arr(2), arr(1), arr(0)) + TimeSerial(arr(3), arr(4), arr(5) + 10)
        .Offset(0, 1).Value = d
        .Offset(0, 1).NumberFormat = "dd:mm:yyyy hh:mm:ss"
    End With

End Sub

enter image description here

您可以根据您的代码进行修改。