【问题标题】:Excel VBA Add-In code throws type mismatch errorExcel VBA 加载项代码引发类型不匹配错误
【发布时间】:2015-09-29 15:57:51
【问题描述】:

我们开发了一个插件,可以将帐号/名称和成本中心编号/名称等内容从我们的新系统翻译到旧系统,反之亦然。它一直有效,直到今天我开始收到类型不匹配错误(运行时错误 13)。

我们的插件有多个工作表。 1 张用于分支机构,1 张用于帐户等。用于进行翻译的表格访问相应的表格以检索数据以进行翻译。以下是几个变量及其声明方式:

Public UB as long
Public ThisAddIn as string

ThisAddIn = "TranslateAddIn.xla"

'Below is the part of the code that is causing the error.

UB = Workbooks(ThisAddIn).Worksheets("Branches").Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row

我检查了“分支机构”表,并没有发现此表上的数据有任何异常。

我不确定发生了什么。此代码已运行多年,没有任何问题。

任何建议将不胜感激。感谢您的帮助。

【问题讨论】:

  • 也许行是一个整数?也许尝试将 UB 定义为变体,看看是否可以解决?
  • @tomatosource IntegerLong 都保存数字值,只有一个分配的内存量更大,因此可以保存更大的数字 - 在引用行号时,您应该始终使用 Long因为Integer只是一个16位的数据类型,所以任何超过32767的数字都会导致溢出错误
  • @MacroMan 谢谢你,很高兴知道。不确定“类型刚性”vba 是怎样的。

标签: excel type-mismatch vba


【解决方案1】:

尝试在您的 find 方法中完全限定范围对象。比如:

Public UB as long
Public ThisAddIn as string

ThisAddIn = "TranslateAddIn.xla"

'Below is the part of the code that is causing the error.

With Workbooks(ThisAddIn).Worksheets("Branches")
    UB = .Cells.Find("*", .Range("A1"), , , xlByRows, xlPrevious).Row
End With

【讨论】:

  • ........谢谢,我要试试看,看看是否能解决我的问题.....
【解决方案2】:

很遗憾,您不能通过名称引用其他工作簿中的工作表,您应该改用索引。

【讨论】:

  • 只要工作表对象用工作簿对象限定,就可以使用工作表名来引用它。
猜你喜欢
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多