我正在尝试将时间工作的数据库粘贴到excel文件中,该文件将自动计算每个作业花费的时间以及总工作时间。当我一次复制/粘贴一个单元格时,一切正常。当我尝试复制2个或更多单元格时,出现以下错误:“运行时错误'13':类型不匹配”
Tlen = Len(Target)
以下是我正在使用的屏幕截图:https://youtu.be/Qhd37U3SLQU
我最终想做的是在视频的1:05。
本质上,我试图将数据库中的信息块粘贴到我的excel文件中,并过滤掉所有没有时间的内容。 D列计算开始和停止时间之间的持续时间,E2将具有D列的总和。
我当前的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsNumeric(Target) Then
'do nothing
End If
If Not Intersect(Target, Range("a2:c900" & ThisWorkbook.Worksheets(1).UsedRange.Rows.Columns.Count)) Is Nothing Then
Tlen = Len(Target)
Targval = Target.Value
If Tlen = 1 Then
TimeV = TimeValue("00:" & Target)
ElseIf Tlen = 2 And Targval < 60 Then
TimeV = TimeValue("00:" & Target)
ElseIf Tlen = 2 And 60 <= Targval Then
'do nothing
ElseIf Tlen = 3 And Targval >= 160 And Targval <= 199 Or Targval >= 260 And Targval <= 299 Or Targval >= 360 And Targval <= 399 Or Targval >= 460 And Targval <= 499 Then
'do nothing
ElseIf Tlen = 3 And Targval >= 560 And Targval <= 599 Or Targval >= 660 And Targval <= 699 Or Targval >= 760 And Targval <= 799 Or Targval >= 860 And Targval <= 899 Then
'do nothing
ElseIf Tlen = 3 And Targval >= 960 And Targval <= 999 Then
'do nothing
ElseIf Tlen = 3 Then
TimeV = TimeValue(Left(Target, 1) & ":" & Right(Target, 2))
ElseIf Tlen = 4 And Targval >= 1060 And Targval <= 1099 Or Targval >= 1160 And Targval <= 1199 Or Targval >= 1260 And Targval <= 1299 Or Targval >= 1360 And Targval <= 1399 Then
'do nothing
ElseIf Tlen = 4 And Targval >= 1460 And Targval <= 1499 Or Targval >= 1560 And Targval <= 1599 Or Targval >= 1660 And Targval <= 1699 Or Targval >= 1760 And Targval <= 1799 Then
'do nothing
ElseIf Tlen = 4 And Targval >= 1860 And Targval <= 1899 Or Targval >= 1960 And Targval <= 1999 Or Targval >= 2060 And Targval <= 2099 Or Targval >= 2160 And Targval <= 2199 Then
'do nothing
ElseIf Tlen = 4 And Targval >= 2260 And Targval <= 2299 Or Targval >= 2360 And Targval <= 2399 Then
'do nothing
ElseIf Tlen = 4 And Targval < 2400 Then
TimeV = TimeValue(Left(Target, 2) & ":" & Right(Target, 2))
ElseIf Tlen >= 4 And Targval >= 2400 Then
'do nothing
End If
Target.NumberFormat = "HHMM"
Application.EnableEvents = False
Target = TimeV
Application.EnableEvents = True
End If
End Sub
答案 0 :(得分:0)
在没有示例数据和预期结果的情况下提供您现有的代码会有些混乱,但是我相信这是您要尝试的事情。
从Base 10取一个1到4位数字,并将其转换为HHMM格式的时间。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a:c"), Me.UsedRange) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("a:c"), Me.UsedRange)
If IsNumeric(t) And LCase(t.NumberFormat) <> "hhmm" And LCase(t.NumberFormat <> "[hh]mm") Then
t.NumberFormat = "[hh]mm"
t = TimeSerial(Int(t.Value / 100), (t.Value Mod 100), 0)
'alternate
't = TimeSerial(Int(val(t.text) / 100), (val(t.text) Mod 100), 0)
't.NumberFormat = "[hh]mm"
End If
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub