持续收到运行时错误“ 13”:类型不匹配

时间:2018-08-24 17:09:13

标签: excel vba excel-vba

我一直收到运行时错误'13'类型不匹配的信息,我不知道为什么。我有一个工作表(Sheet1),上面有我要复制到另一工作表(工作表6)的数据,仅当列中的信息包含USA时。它还会先清除其他工作表(工作表6)的数据,因为我每周都会更新工作表Sheet1上的信息,因此工作表6也需要更新。我不知道为什么我的if语句上出现类型不匹配。

    Private Sub CommandButton1_Click()

Worksheets("Sheet6").Activate
ActiveSheet.Rows("3:" & Rows.Count).ClearContents

 Worksheets("Sheet1").Activate
 With Worksheets("Sheet1")
     a = .Cells(.Rows.Count, 1).End(xlUp).Row


End With


For i = 2 To a
     Worksheets("Sheet1").Activate
     If ActiveSheet.Cells(i, 5).Value = "USA" Then
     ActiveSheet.Rows(i).Copy

     Worksheets("Sheet6").Activate
     With Worksheets("Sheet6")
     b = .Cells(.Rows.Count, 1).End(xlUp).Row
     ActiveSheet.Cells(b + 1, 1).Select
     ActiveSheet.Paste
     End With
     Worksheets("Sheet1").Activate



     End If

     Next

    Application.CutCopyMode = False
     ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub

1 个答案:

答案 0 :(得分:0)

您是否尝试过在评估之前将单元格数据转换为字符串(CSTR):

Private Sub CommandButton1_Click()

Worksheets("Sheet6").Activate
ActiveSheet.Rows("3:" & Rows.Count).ClearContents

 Worksheets("Sheet1").Activate
 With Worksheets("Sheet1")
     a = .Cells(.Rows.Count, 1).End(xlUp).Row


End With


For i = 2 To a
     Worksheets("Sheet1").Activate
If CSTR(ActiveSheet.Cells(i, 5).Value) = "USA" Then 
     ActiveSheet.Rows(i).Copy

     Worksheets("Sheet6").Activate
     With Worksheets("Sheet6")
     b = .Cells(.Rows.Count, 1).End(xlUp).Row
     ActiveSheet.Cells(b + 1, 1).Select
     ActiveSheet.Paste
     End With
     Worksheets("Sheet1").Activate



     End If

     Next

    Application.CutCopyMode = False
     ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub