Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 错误13!!类型不匹配_Vba_Excel - Fatal编程技术网

Vba 错误13!!类型不匹配

Vba 错误13!!类型不匹配,vba,excel,Vba,Excel,有什么想法吗???我不知道为什么会出现类型错误,特别是我更改了两个单元格的类型 Cells(i, 7).NumberFormat = "@" Workbooks("template.xls").Worksheets("Introduction").Cells(j, 21).NumberFormat = "@" If Left(Cells(i, 7), 13) = Workbooks("template.xls").Worksheets("Introduction").Cells(j, 21).V

有什么想法吗???我不知道为什么会出现类型错误,特别是我更改了两个单元格的类型

Cells(i, 7).NumberFormat = "@"
Workbooks("template.xls").Worksheets("Introduction").Cells(j, 21).NumberFormat = "@"
If Left(Cells(i, 7), 13) = Workbooks("template.xls").Worksheets("Introduction").Cells(j, 21).Value  
.
.
.
注释代码:

Dim i As Long
Dim j As Integer
For j = 5 To derlig
    For i = 2 To 4000
        Cells(i, 2).NumberFormat = "@"
        Workbooks("template.xls").Worksheets("Introduction").Cells(j, 21).NumberFormat = "@"
        Workbooks("Cat export.xls").Worksheets("Items").Activate
        If Left(Cells(i, 2), 13).Value = Workbooks("template.xls").Worksheets("Introduction").Cells(j, 21).Value Then
            Workbooks("Cat export.xls").Worksheets("Items").Cells(i, 3) = Right(Workbooks("Cat export.xls").Worksheets("Items").Cells(i, 2), 5)
        End If
    Next
Next

发生此错误的原因是您没有限定所有对象,因此如果任何工作簿或工作表处于活动状态,而不是您预期的状态,则代码可能无法正确执行

最佳实践是始终限定VBA中的对象并直接使用它们

见下文:

Dim wbMyWB as Workbook, wbTemplate as Workbook
Set wbMyWB = Workbooks("myWB.xlsx") 'change as needed
Set wbTemplate = Workbooks("template.xls")

Dim wsMyWS as Worksheet, wsIntro as Worksheet
Set wsMyWS = wbMyWB.Worksheets("Sheet1") ' change as needed
Set wsIntro = wbTemplate.Worksheets("introduction")

'....
Dim rMyRange as Range, rIntro as Range

'assumes i and j are properly set to long or integer (or variant (hopefully not) type 
'ideally i and j are both set to integer
rMyRange = wsMyWs.Cells(i,7) 
rIntro = wsIntro.Cells(j,21)

rMyRange.NumberFormat = "@"
rIntro.NumberFormat = "@"

If Left(rMyRange,13).Value = rIntro.Value Then 
   'condition satisfied
End If

您能否举例说明
单元格(i,7)
工作簿(“template.xls”)。工作表(“简介”)。单元格(j,21)
?还要再次检查,
工作簿(“template.xls”)
是否打开,是吗?添加代码
debug.print Left(单元格(i,7),13)和“=”&debug.print工作簿(“template.xls”)。工作表(“简介”)。单元格(j,21).Value
然后按ctrl+G,查看结果是否符合预期。在
if
语句的左侧添加您要检查的工作簿和工作表,就像您在右侧所做的那样。除了上述人员提出的问题外,您如何声明
i
j
?它们的值是什么?@Jordan Cells(i,7)=BF-1CI-10200-00033工作簿(“template.xls”)。工作表(“简介”)。Cells(j,21)=BJ-1CI-35020-类型不匹配?同意@SiddharthRout-我可能已经跳过了枪,变量
i
j
的设置方式应该明确考虑。我将再次修改或删除OP.@Ikramebahar-你得到了什么错误,在哪一行?@ScottHoltzman现在我在if行
Dim I中得到424个错误,只要Dim j为整数,j=5,到derlig,I=2到4000个单元格(I,2)。NumberFormat=“@”工作簿(“template.xls”)。工作表(“简介”)。单元格(j,21).NumberFormat=“@”如果左(单元格(i,2),13)。值=工作簿(“template.xls”)。工作表(“简介”)。单元格(j,21)。值然后是工作簿(“Cat export.xls”)。工作表(“Items”)。单元格(i,3)=右(工作簿(“Cat export.xls”)。工作表(“Items”)。单元格(i,2),5)如果Next
@ikramebahar,则结束-您仍然没有限定所有对象。。。例如
单元格(i,2).NumberFormat=“@”
。。。或
左侧(单元格(i,2),13)。值