Excel VBA Object Variable or with variable not set

Hi
I get the error "Object Variable or with variable not set" at the line marked 'xxxxxx
I am trying trying to loop through the list created in UniqueList
Sub Test()
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
dic = UniqueList(Range("C2:C12000")) 'xxxxxx

For Each Key In dic.Keys
    MsgBox "Key: " & Key & " Value: " & dic(Key)
Next
End Sub

Public Function UniqueList(rngInput As Range) As Variant
   Dim rngCell           As Range
   Dim dic           As Object
   Dim lngRow            As Long

   Set dic = CreateObject("Scripting.Dictionary")
   
   For Each rngCell In rngInput.Cells
      If Len(rngCell.Value) <> 0 Then dic(CStr(rngCell.Value)) = Empty
    Next rngCell
   UniqueList = dic.Keys
End Function

Open in new window


Prior related question: http:Q_28643756.html
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ken ButtersCommented:
whenever you assign something to an object you always have to use the word "Set".

You have this line of code :

dic = UniqueList(Range("C2:C12000")) 'xxxxxx

"dic" is declared as an object, so to assign an object to it... you must prefix the assignment with "Set".

Correction shown below.

Sub Test()
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
Set dic = UniqueList(Range("C2:C12000")) 'xxxxxx

For Each Key In dic.Keys
    MsgBox "Key: " & Key & " Value: " & dic(Key)
Next
End Sub

Public Function UniqueList(rngInput As Range) As Variant
   Dim rngCell           As Range
   Dim dic           As Object
   Dim lngRow            As Long

   Set dic = CreateObject("Scripting.Dictionary")
   
   For Each rngCell In rngInput.Cells
      If Len(rngCell.Value) <> 0 Then dic(CStr(rngCell.Value)) = Empty
    Next rngCell
   UniqueList = dic.Keys
End Function

Open in new window

0
aikimarkCommented:
Since the UniqueList() function returns an array, use this version of the Test() routine:
Option Explicit


Sub Test()

Dim vKeys As Variant
Dim vItem as Variant
vKeys = UniqueList(Range("C2:C12000"))

For Each vItem In vKeys
    MsgBox "Key: " & vItem
Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
That worked well. Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.