English 中文(简体)
Excel VBA:Rong-time差错 424 。 所需反对
原标题:Unexpected error in Excel VBA: Run-time error 424 . Object required

I have a subroutine that adds an item to a Collection only if that item is not present. This effectively treats Collections as if they are sets. But VBA returns error 424 when the subroutine is called.

Private Sub SetUnion(ByVal e As String, ByVal Coll As Collection)
found = False
For n = 1 To Coll.Count
    If Coll(n) = e Then
        found = True
        Exit For
    End If
Next n
If found = False Then Coll.Add e
End Sub

当我打电话给我时

Dim s1, C  As Collection
For n = 1 To s1.Count
SetUnion s1(n), C   #
Next n

Run-time error ‘424’:
Object required

我做了哪些错误?

问题回答
  • @braX评论说,您在《家庭、社会、文化权利国际公约》法典中使用了另一种方案拟定语言的辛加语。

  • 根据你的法典,你似乎再次试图从扼杀中摘取独特的特性。

Option Explicit

Sub SetUnion(ByVal e As String, ByRef Coll As Collection)
    Dim n As Long
    If Not Coll Is Nothing Then
        For n = 1 To Coll.Count
            If Coll(n) = e Then
                Exit Sub
            End If
        Next n
    End If
    Coll.Add e
End Sub

Sub test()
    Dim sStr As String, oCol  As Collection, n As Long
    sStr = "abcabd"
    Set oCol = New Collection
    For n = 1 To Len(sStr)
        SetUnion Mid(sStr, n, 1), oCol
    Next n
    For n = 1 To oCol.Count
        Debug.Print oCol(n)
    Next
End Sub

产出:

a
b
c
d

  • Use less code lines with Dictionary object
Sub test2()
    Dim sStr As String, vKey
    Dim objDic As Object, n As Long
    Set objDic = CreateObject("scripting.dictionary")
    sStr = "abcabd"
    For n = 1 To Len(sStr)
        objDic(Mid(sStr, n, 1)) = ""
    Next n
    For Each vKey In objDic
        Debug.Print vKey
    Next
End Sub




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签