错误13 !!类型不匹配

时间:2016-07-29 14:40:26

标签: excel vba excel-vba

任何想法???我不知道为什么会出现类型错误,尤其是我改变了两个单元格的类型

const index = _.findIndex(state.items, { id: action.newItem.id });
return {
  ...state,
  items: [
    ...state.items.slice(0, index),
    { ...state.items[index], ...action.newItem },
    ...state.items.slice(index + 1)
  ]
};

评论代码:

''

1 个答案:

答案 0 :(得分:2)

发生错误是因为您没有资格对象的所有,因此,如果任何工作簿或工作表处于活动状态而非您所期望的状态,则代码可能无法正常执行。

最佳做法 始终限定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