类型不匹配VBA检查单元格值

时间:2015-12-22 10:42:21

标签: excel vba excel-vba

我想了解为什么会出现类型不匹配错误:

这是我的功能,基本上它是从工作表复制到另一个,然后删除复制的单元格的第一个字符:

Sub copyBackFormulas()

Application.ScreenUpdating = False
Application.EnableEvents = False

'iterate through all worksheets
Dim WS_Count As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
Dim I As Integer


For I = 1 To WS_Count
    Dim ws1 As Worksheet
    Set ws1 = ThisWorkbook.Worksheets(I)
    'if sheet contains evdre
    Set d = ws1.Cells.Find("EVDRE:OK")
    If Not d Is Nothing Then
        'copy back all formulas except from current view
        Dim wsTarget As Worksheet
        Set wsTarget = ws1
        nameHidden = ActiveSheet.Name & "_BPCOffline"
        Sheets(nameHidden).Visible = True
        Dim wsSource As Worksheet
        Set wsSource = Sheets(nameHidden)

        For Each c In wsSource.UsedRange.Cells
            If Left(c.Value, 1) = "_" Then
                    If Left(c.Value, 7) = "_=EVCVW" Then
                    Else
                        c.Copy wsTarget.Range(c.Address)
                    End If
            End If
        Next
        'Remove underscore
        For Each c In wsTarget.UsedRange.Cells
            If Left(c.Value, 1) = "_" Then
                c.Formula = Right(c.Value, Len(c.Value) - 1)
            End If
        Next
        wsSource.Visible = xlSheetHidden
    End If
    Range("A1").Select
Next I

Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

我有几张可能需要复制的表格。关键是我在行上遇到类型不匹配错误:If Left(c.Value, 1) = "_" Then 但是,如果我从其他工作表开始运行宏,它只是完美地运行,或者只在其中一个工作表上执行正确的操作而不是其他工作表。 我不明白是什么让它在某些时候发挥作用,什么不是。 任何输入都非常受欢迎

编辑:我认为这个问题与宏可能找不到第一个条件If Left(c.Value, 1) = "_" Then

的事实有关

1 个答案:

答案 0 :(得分:0)

您无法复制具有错误值的粘贴公式

如果你想跳过有错误的单元格,你需要另一个If ... End if block:

If Not Iserror(c.Value) Then
...
End if

正如Rory在评论中所解释的那样