Application.Clean on Variant / String type mismatch

时间:2017-10-13 00:45:44

标签: excel vba excel-vba

我有一段代码以前有效但不再有用。我以为我搞砸了另一个部分,但经过几个小时的调试后,我发现了一些非常奇怪的结果。希望我简单地修改代码,这是一个简单的修复。

不再有效的原始代码:

Private Function CleanColumn(ByRef Column As Range)
    Dim MyCells() As Variant
    MyCells = Column.SpecialCells(xlCellTypeConstants, 23).Cells.Value
    MyCells = Application.Clean(MyCells)
    Column.SpecialCells(xlCellTypeConstants, 23).Cells.Value = MyCells
End Function

它只是返回错误13,类型不匹配。这是针对47k的记录,所以我几乎不可能逐个查找问题,所以我重写了以下内容以帮助诊断:

Private Function CleanColumn(ByRef Column As Range)
    Dim MyCells() As Variant
    MyCells = Column.SpecialCells(xlCellTypeConstants, 23).Cells.Value

    Dim MyCell As Variant
    For Each MyCell In MyCells
        If VarType(MyCell) = vbString And Not IsNull(MyCell) And Not Left(MyCell, 4) = "=HYPE" Then
            MyCell = Application.Clean(MyCell)
        Else
            MsgBox 911
        End If
    Next
    Column.SpecialCells(xlCellTypeConstants, 23).Cells.Value = MyCells
End Function

MsgBox 911永远不会执行,也不会在该行上设置断点。代码按预期工作。

想到我疯了,我又像这样添加了MyCells = Application.Clean(MyCells)

Private Function CleanColumn(ByRef Column As Range)
    Dim MyCells() As Variant
    MyCells = Column.SpecialCells(xlCellTypeConstants, 23).Cells.Value

    Dim MyCell As Variant
    For Each MyCell In MyCells
        If VarType(MyCell) = vbString And Not IsNull(MyCell) And Not Left(MyCell, 4) = "=HYPE" Then
            MyCell = Application.Clean(MyCell)
        Else
            MsgBox 911
        End If
    Next
    MyCells = Application.Clean(MyCells)
    Column.SpecialCells(xlCellTypeConstants, 23).Cells.Value = MyCells
End Function

MsgBox 911上的断点仍未点击,循环没有异常,但Application.Clean(MyCells)上仍然是例外。

请告诉我,我错过了一些明显的东西?

0 个答案:

没有答案
相关问题