VBA将项目添加到SortedList会导致“自动化错误”

发布于 2025-01-23 19:01:39 字数 219 浏览 0 评论 0原文

为什么以下代码会导致错误:-2146233079(80131509)。

Sub testSortedList()
    Dim list
    Set list = CreateObject("System.Collections.SortedList")
    list.Add 1978340499, "a"
    list.Add 1, "b"
End Sub

Why the following code causes an error: -2146233079(80131509).

Sub testSortedList()
    Dim list
    Set list = CreateObject("System.Collections.SortedList")
    list.Add 1978340499, "a"
    list.Add 1, "b"
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等往事风中吹 2025-01-30 19:01:39

它们的列表的键必须具有相同的数据类型。您添加了两种不同的数据类型,第一种是类型long,类型integer的第二个,这会引发错误消息 *无法比较两个元素...

”解决:将&附加到1,这将迫使VBA将您的常数存储为long

list.Add 1&, "b"

或为键使用变量值:

Dim key as Long
key = 1
list.Add key, "b"

They keys of the list needs to be of same data type. You add two different data types, the first is of type Long, the second of type Integer and this throws the error message *failed to compare two elements..."

Simplest work around: Append an & to the 1, this will force VBA to store your constant as a Long:

list.Add 1&, "b"

Or use variables for your key values:

Dim key as Long
key = 1
list.Add key, "b"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文