遇到VBA" TYPE MISMATCH"错误

时间:2015-03-11 01:37:03

标签: vba excel-vba excel

我正在编写一个VBA项目,我将一列文件目录存储在一个隐藏的单元格中,系统将逐个循环遍历这些单元格,然后进入绝对目录路径以检查是否存在。如果文件存在,则选中该复选框并显示时间戳。然后,如果时间戳超过截止时间,则时间戳将更改为红色字体颜色。但是我遇到了上面的问题,我怀疑这是我的日期格式,但我尝试过,但我仍然迷失在如何编辑我的代码。因此,我寻求你的亲切,帮助我看看我能做些什么。感谢。

Sub test5()
Dim MyFile As String
Dim FinalRow As Long
Dim Row As Long
Dim i As Integer
Dim d As Integer
d = 3
i = 1
FinalRow = Range("A65536").End(xlUp).Row
For Row = 1 To FinalRow
     If Not IsEmpty(ActiveSheet.Cells(Row, "S")) Then
            MyFile = ActiveSheet.Cells(Row, "S").Value
            If Dir(MyFile) <> "" Then
                ActiveSheet.OLEObjects("CheckBox" & i). _
                Object.Value = True
                ActiveSheet.Cells(d, "F").Value = Format(Now, "dd-mm-yy")
                If (ActiveSheet.Cells(d, "F") - ActiveSheet.Cells(d, "G") >= 0) Then
                    ActiveSheet.Cells(d, "F").Font.Color = vbRed
                End If
                i = i + 1
                d = d + 1
            End If
     End If
Next
End Sub

非常感谢,我已经解决了这个问题。相反,我已将MonthView对象添加到userform中以简化操作。所以就像弹出日历一样,用户可以选择日期。但现在我遇到了另一个问题。解决日期问题后,我的检查文件循环无法正常工作。它可以检查第一个复选框,在该复选框之后,即使文件存在,也无法找到或循环任何后续文件。它与对象有什么关系,还是我的算法逻辑?

3 个答案:

答案 0 :(得分:1)

您的ActiveSheet.Cells(d, "F").Value = Format(Now, "dd-mm-yy")已将ActiveSheet.Cells(d, "F")转换为文字,因此无法进行算术运算。

您应该使用ActiveSheet.Cells(d, "F").Value = Now并将其NumberFormat更改为“dd-mm-yy”。

如果不属于IF F > G,您应该撤消将颜色从红色更改为黑色(或默认颜色)。

尝试以下:

Sub test6()
    Dim MyFile As String
    Dim FinalRow As Long
    Dim Row As Long
    Dim i As Integer
    Dim d As Integer
    d = 3
    i = 1
    FinalRow = Cells(Rows.Count, "A").End(xlUp).Row
    For Row = 1 To FinalRow
         If Not IsEmpty(ActiveSheet.Cells(Row, "S")) Then
                MyFile = ActiveSheet.Cells(Row, "S").Value
                If Dir(MyFile) <> "" Then
                    ActiveSheet.OLEObjects("CheckBox" & i). _
                    Object.Value = True
                    With ActiveSheet.Cells(d, "F")
                        .Value = Now
                        .NumberFormat = "dd-mm-yy"
                        'If (ActiveSheet.Cells(d, "F") - ActiveSheet.Cells(d, "G") >= 0) Then
                        '    ActiveSheet.Cells(d, "F").Font.Color = vbRed
                        'End If
                        If (.Value - .Offset(0, 1).Value) >= 0 Then
                            .Font.Color = vbRed
                        Else
                            .Font.Color = vbBlack
                        End If
                    End With
                    i = i + 1
                    d = d + 1
                End If
         End If
    Next
End Sub

答案 1 :(得分:0)

Cells(d, "F")Cells(d, "G")中的某些内容不是数字 - 或者至少不能解析为数字;可能是其中一个单元格中的值没有值,或者是文本内容。

答案 2 :(得分:0)

谢谢大家。我解决了下面是代码。归功于帕特里克。万分感谢。

Sub test5()
    Dim MyFile As String
    Dim FinalRow As Long
    Dim Row As Long
    Dim i As Integer
    Dim d As Integer
    d = 2
    i = 0
    FinalRow = Cells(Rows.count, "S").End(xlUp).Row
    For Row = 3 To FinalRow
         If Not IsEmpty(ActiveSheet.Cells(Row, "S")) Then
                i = i + 1
                d = d + 1
                MyFile = ActiveSheet.Cells(Row, "S").Value
                If Dir(MyFile) <> "" Then
                    ActiveSheet.OLEObjects("CheckBox" & i). _
                    Object.Value = True
                    With ActiveSheet.Cells(d, "F")
                        .Value = Now
                        .NumberFormat = "dd-mm-yy"
                        'If (ActiveSheet.Cells(d, "F") - ActiveSheet.Cells(d, "G") >= 0) Then
                        '    ActiveSheet.Cells(d, "F").Font.Color = vbRed
                        'End If
                        If (.Value - .Offset(0, 1).Value) >= 0 Then
                            .Font.Color = vbRed
                        Else
                            .Font.Color = vbBlack
                        End If
                    End With
                   ' i = i + 1
                    'd = d + 1
                End If
         End If
    Next
End Sub

但有没有办法代码绕过错误400?