为什么 VBA 突然在使用动态 String 变量作为键的字典项上抛出运行时错误 457?

问题描述 投票:0回答:1

过去几天我一直在 Excel 中运行以下 VBA 代码,突然,我开始收到以下错误:

“运行时错误457: 该键已与该集合的一个元素关联”,

始终将 [infantry_dic.Add cell_str, subCell_str] 行识别为问题,即使我注释掉所有其他字典和集合 .Add 调用。有趣的是,当我第一次运行备份文件时,我没有收到错误,但在后续运行中,如果不更改任何内容,则会返回错误消息。这是某种持久内存问题吗?我重新启动甚至关闭了我的笔记本电脑,但问题仍然存在。

代码非常简单,即使运行时的备份现在也无法运行,除非运行时错误停止流程。这让我发疯!

Option Explicit
Public leaders_col As New Collection
Public cavalry_dic As New Scripting.Dictionary
Public infantry_dic As New Scripting.Dictionary
Public artillery_dic As New Scripting.Dictionary
Public defenses_col As New Collection

Private Sub Workbook_Open()
    
    Dim i As Integer
    Dim j As Integer
    Dim cell_str As String
    Dim subCell_str As String
    
    For i = 7 To 11
        cell_str = "C" & CStr(i)
        leaders_col.Add cell_str
    Next i
    For i = 17 To 26
        subCell_str = ""
        If IsEmpty(Range("F" & i)) Then
            cell_str = "C" & CStr(i)
            For j = i + 1 To i + 10
                If Not IsEmpty(Range("F" & j)) And IsEmpty(Range("B" & j)) Then
                    If j = i + 1 Then
                        subCell_str = "D" & CStr(j)
                    Else
                        subCell_str = subCell_str & ",D" & CStr(j)
                    End If
                Else
                    Exit For
                End If
                i = j
            Next j
        Else
            cell_str = "C" & CStr(i)
        End If        
        cavalry_dic.Add cell_str, subCell_str    
    Next i
    For i = 30 To 53
        subCell_str = ""
        If IsEmpty(Range("F" & i)) Then
            cell_str = "C" & CStr(i)
            For j = i + 1 To i + 10
                If Not IsEmpty(Range("F" & j)) And IsEmpty(Range("B" & j)) Then
                    If j = i + 1 Then
                        subCell_str = "D" & CStr(j)
                    Else
                        subCell_str = subCell_str & ",D" & CStr(j)
                    End If
                Else
                    Exit For
                End If
                i = j
            Next j
        Else
            cell_str = "C" & CStr(i)
        End If      
        infantry_dic.Add cell_str, subCell_str     
    Next i
    For i = 57 To 70
        subCell_str = ""
        If IsEmpty(Range("F" & i)) Then
            'Add the unit as a main unit
            cell_str = "C" & CStr(i)
            For j = i + 1 To i + 10
                If Not IsEmpty(Range("F" & j)) And IsEmpty(Range("B" & j)) Then
                    If j = i + 1 Then
                        subCell_str = "D" & CStr(j)
                    Else
                        subCell_str = subCell_str & ",D" & CStr(j)
                    End If
                Else
                    Exit For
                End If
                i = j
            Next j
        Else
            cell_str = "C" & CStr(i)
        End If      
        artillery_dic.Add cell_str, subCell_str    
    Next i
    For i = 76 To 80
        cell_str = "C" & CStr(i)
        defenses_col.Add cell_str
    Next i  
End Sub
excel vba dictionary runtime-error
1个回答
0
投票

答案:相信你的错误警告,哈哈。这是一个简单的加法问题,但它并没有像我想象的那样工作。呃。

© www.soinside.com 2019 - 2024. All rights reserved.