简体   繁体   中英

What object has not been set or referenced properly?

Private Sub UserForm_Initialize()
Call CreateDictFromColumns("Schedule", "A", "B")
Dim dic As Dictionary
Set dic = createdDic
For Each k In dic.Keys
    MsgBox dic(k)
Next
With ListBox1
    .AddItem "test"
End With

End Sub

'http://stackoverflow.com/questions/33523658/what-is-the-easiest-way-to-take-two-columns-of-data-and-convert-to-dictionary
Function CreateDictFromColumns(sheet As String, keyCol As String, valCol As String) As Dictionary
    Dim aDict As Dictionary
    Set aDict = New Dictionary
    Dim rng As Range: Set rng = Sheets(sheet).Range(keyCol & ":" & valCol)
    Dim i As Long
    Dim lastCol As Long '// for non-adjacent ("A:ZZ")
    lastCol = rng.Columns.Count
    For i = 1 To rng.Rows.Count
        If (rng(i, 1).Value = "") Then Exit Function
        aDict.Add rng(i, 1).Value, rng(i, lastCol).Value
    Next
    Set createdDic = aDict
End Function

I continually get the error "Object Required", and I believe it is coming from Set dic = createdDic However I cannot seem to figure out as to why this is wrong. Does this object not need to be set this way, or is it set incorrectly when trying to reference a function as a dictionary? All this code is doing is attempting to create a dictionary as the column being the key, and the row being the value stored. Any additional comments would be useful in understanding a little more on how excel works, along with how declaring a dictionary as a function is handled. Thanks.

You should always be using Option Explicit in your modules. This will fail to compile code where variables aren't declared (often the case with typographical errors, etc.)

For example, you have:

Set dic = createdDic

But, createdDic isn't the name of any existing object, object variable, or function which returns an object in the scope of your subroutine UserForm_Initialize .

So what this is doing, is interpreting createdDic as an undeclared variable of type Variant which will, by default, contain an empty string or empty numeric value, which evaluates your expression to:

Set dic = ""  

Or

Set dic = Empty

This raises the Object Required error, because you're attempting to assign a non-object to an object variable.

Possibly confusing you, here you are calling the function (but not returning any value from it):

Call CreateDictFromColumns("Schedule", "A", "B")

TO FIX THIS ERROR

Assign the result of CreateDictFromColumns to the dic object variable, like so:

Set dic = CreateDictFromColumns("Schedule", "A", "B")

And ensure the return value before End Function is given:

Set CreateDictFromColumns = aDict

Putting it all together:

Private Sub UserForm_Initialize()

Dim dic As Dictionary
Set dic = CreateDictFromColumns("Schedule", "A", "B")
For Each k In dic.Keys
    MsgBox dic(k)
Next
With ListBox1
    .AddItem "test"
End With

End Sub

'http://stackoverflow.com/questions/33523658/what-is-the-easiest-way-to-take-two-columns-of-data-and-convert-to-dictionary
Function CreateDictFromColumns(sheet As String, keyCol As String, valCol As String) As Dictionary
    Dim aDict As Dictionary
    Set aDict = New Dictionary
    Dim rng As Range: Set rng = Sheets(sheet).Range(keyCol & ":" & valCol)
    Dim i As Long
    Dim lastCol As Long '// for non-adjacent ("A:ZZ")
    lastCol = rng.Columns.Count
    For i = 1 To rng.Rows.Count
        If (rng(i, 1).Value = "") Then Exit Function
        aDict.Add rng(i, 1).Value, rng(i, lastCol).Value
    Next
    Set CreateDictFromColumns = aDict
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM