“apllication-defined或面向对象的错误”是在编译vb代码时抛出的。 在多个工作簿之间工作

这是代码的一部分。

在第一个,如果出现错误popop说“应用程序定义或面向对象的错误”。

For Row_num = 2 To 3 'Column numbers For Col_num = 6 To 7 'Check for grey color If Workbooks(Book_name).Worksheets("ParameterLifecycles").Range(Cells(Row_num, Col_num), Cells(Row_num, Col_num)).Interior.Color = RGB(217, 217, 217) Then 'Check if software name is empty If Not IsEmpty(Workbooks(Book_name).Worksheets("ParameterLifecycles").Range("S" & Row_num)) Then soft_rd = Workbooks(Book_name).Worksheets("ParameterLifecycles").Range("S" & Row_num) Else soft_rd = Workbooks(Book_name).Worksheets("ParameterLifecycles").Range("B" & Row_num) End If 'Exit the row once software name is obtained Col_num = 16[enter image description here][1] 

Workbooks(Book_name).Worksheets("ParameterLifecycles").Range(Cells(Row_num, Col_num), Cells(Row_num, Col_num)) Cells取自ActiveSheet并可能不从Workbooks(Book_name).Worksheets("ParameterLifecycles")

发生该错误是因为Range的来源与Cells的来源不同。

使用:

 ... With Workbooks(Book_name).Worksheets("ParameterLifecycles") If .Range(.Cells(Row_num, Col_num), .Cells(Row_num, Col_num)).Interior.Color = RGB(217, 217, 217) Then ... End With ... 

请注意Cells之前的点。 所以你可以肯定的是, Cells是从同一张表中取出的。