运行时错误“13”:VBA代码

时间:2015-08-10 19:15:18

标签: excel vba excel-vba excel-2013

我在我正在处理的项目中使用以下代码:

Sub test()
    Application.ScreenUpdating = False
    Worksheets("Technician Report Summary").Range("G2:I8561").ClearContents
    Dim Source As Range, Target As Range
    Dim n As Long, i As Long
    n = Range("C:C").Cells.Count
    i = Cells(n, "C").End(xlUp).Row
    Set Source = Range(Cells(1, "C"), Cells(n, "E"))
    Set Target = Range("G2:I2")

    For i = 2 To n
        If Source.Cells(i, 1).Value <> "Not Used" And Not Application.WorksheetFunction.IsNA(Source.Cells(i, 3).Value) Then
            Source.Rows(i).Copy
            Target.PasteSpecial xlPasteValues
            Set Target = Target.Offset(1)
        End If
    Next i

    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

当我运行代码时,它似乎正常运行,它会按照代码中设置的条件将过滤值列表复制到同一工作表中的另一个位置。问题是我也收到以下错误:

  

运行时错误'13':类型不匹配

我有什么问题?我认为我在声明变量或其他东西方面犯了一些错误,但我对VBA不够熟悉,无法确定问题所在。

任何帮助都将不胜感激。

编辑:

抱歉不清楚。

我在工作表'技术人员报告摘要'中有一组值占据C2:E8561的范围。我想要过滤和复制一些错误的值,这些是“未使用”和“#N / A”错误。上面的代码复制了这些值,然后将它们粘贴到原始未过滤的值集旁边,并在G,H和I列中删除了“未使用”和“#N / A”的实例。

当我运行它时,没有突出显示的行是问题,错误对话框刚刚弹出,当我点击“确定”时,没有任何内容突出显示。

编辑2:

我做了@KFitchter建议的改变,现在似乎锁定了。我点击了转义键,突出显示了以下行:

If Source.Cells(i, 1).Text <> "Not Used" And Not Application.WorksheetFunction.IsNA(Source.Cells(i, 3).Value) Then

编辑3:

想到其他可能搞乱它的东西。列C,D和E中的值实际上是从工作簿中的其他位置拉取值的函数。以下是其中一个功能的示例。

  

= INDEX('RAW DATA'!$ A:$ A,$ B2)

2 个答案:

答案 0 :(得分:1)

所以我不确定问题是什么。

我将代码修改了一小部分(使用我的原始猜测):

Sub test()
    Application.ScreenUpdating = False
    Worksheets("Technician Report Summary").Range("G2:I8561").ClearContents
    Dim Source As Range, Target As Range
    Dim n As Long, i As Long
    n = Range("C:C").Cells.Count
    i = Cells(n, "C").End(xlUp).Row
    Set Source = Range(Cells(1, "C"), Cells(n, "E"))
    Set Target = Range("G2:I2")

    For i = 2 To n
        If Source.Cells(i, 1).Text <> "Not Used" And Not Application.WorksheetFunction.IsNA(Source.Cells(i, 3).Value) Then
            Source.Rows(i).Copy
            Target.PasteSpecial xlPasteValues
            Set Target = Target.Offset(1)
        End If
    Next i

    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

唯一真正的区别是&#34;价值&#34;到&#34;文字&#34;。我没有收到任何错误。唯一的问题是它花了很长时间才能做它应该做的事情,因为线路太多了。

现在...... 我建议你做一些与众不同的事情。

Sub test()
    Application.ScreenUpdating = False

    Worksheets("Technician Report Summary").Range("G2:I8561").ClearContents

    Dim Source As Range, Target As Range
    Dim n As Long

    ActiveSheet.Range("C:E").AutoFilter Field:=1, Criteria1:= _
        Array("<>#N/A", "<>Not Used"), Operator:=xlAnd
    ActiveSheet.Range("C:E").AutoFilter Field:=3, Criteria1:= _
        "<>#N/A", Operator:=xlAnd

    n = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row

    Set Source = Range("C2:E" & n).SpecialCells(xlCellTypeVisible)
    Set Target = Range("G2")

    Source.Copy
    Target.PasteSpecial (xlPasteValues)

    Application.CutCopyMode = False
    Application.ScreenUpdating = True

    Range("C:E").AutoFilter
End Sub

这基本上是你想要做的,但是使用自动过滤器。它比循环遍历每个单元格要快得多,并且最终做同样的事情。我希望有帮助...

答案 1 :(得分:0)

首先指定所有范围和单元格引用的父级。

不要将错误检查与其他字符串比较或布尔检查结合使用。首先检查错误,然后在新代码行上处理其他布尔检查。

n = .Range("C:C").Cells.Count行将1,048,576放入 n 。你真的想要Set src = .Range("C1:E1048576)For i = 2 To 1048576吗?

直接文本比较通常区分大小写。

尽可能使用直接值传输复制,选择性粘贴,值。

将所有这些点放在一起:

Sub test()
    Dim src As Range, trgt As Range, nas As Range
    Dim n As Long, i As Long, bERR As Boolean

    Application.ScreenUpdating = False

    With Worksheets("Technician Report Summary")
        .Range("G2:I8561").ClearContents
        n = .Cells(Rows.Count, "C").End(xlUp).Row
        Set src = .Range(.Cells(1, "C"), .Cells(n, "E"))
        Set trgt = .Range("G2:I2")
    End With

    For i = 2 To n
        If Not IsError(src.Cells(i, 3)) Then
            Debug.Print src.Cells(i, 3).Text
            If LCase(src.Cells(i, 1).Value2) <> "not used" Then
                trgt = src.Rows(i).Value
                Set trgt = trgt.Offset(1)
            End If
        End If
    Next i

    Application.ScreenUpdating = True
End Sub

这应该足以让你工作。

相关问题