Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 385
Default Scripting.Dictionary question

With the following code i keep getting this error. Can you help?
***Compile Error
***User_defined not type defined

Private Sub LoadMares()
Dim mares As New Scripting.Dictionary (THIS IS WHAT IS HIGHLIGHTED)
Dim index As Long
Dim Mare As String
For index = 2 To Source.Rows.Count
Mare = Source.Cells(index, 1)
If Not mares.Exists(Mare) Then
mares.Add Mare, Mare
cmbMare.AddItem Mare
End If
Next


End Sub
--
Though daily learning, I LOVE EXCEL!
Jennifer
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Scripting.Dictionary question

Jennifer,
You need to add a reference (ToolsReferences) to "Microsoft Scripting
Runtime".
Depending what you are doing, you may not need this at all, as VBA has a
Collection object which behaves similar to the Dictionary object.

NickHK
P.S. You should aware that using "New" this way:
Dim mares As New Scripting.Dictionary
will mean that
If mares Is Nothing Then
will never evaluate to True. This is because an instance of the class will
be created everytime the variable "mares" is referred to, if it does not
currently refer to an instance.
Using the 2 part:
Dim mares As Scripting.Dictionary
Set mares = New Scripting.Dictionary
avoids this.


"Jennifer" wrote in message
...
With the following code i keep getting this error. Can you help?
***Compile Error
***User_defined not type defined

Private Sub LoadMares()
Dim mares As New Scripting.Dictionary (THIS IS WHAT IS HIGHLIGHTED)
Dim index As Long
Dim Mare As String
For index = 2 To Source.Rows.Count
Mare = Source.Cells(index, 1)
If Not mares.Exists(Mare) Then
mares.Add Mare, Mare
cmbMare.AddItem Mare
End If
Next


End Sub
--
Though daily learning, I LOVE EXCEL!
Jennifer



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Scripting.Dictionary question

Also it is bad practice to declare an object as New, it may get created when
you least expect it, far better to us

Dim mares As Scripting.Dictionary

Set mares = New Scripting.Dictionary


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Jennifer" wrote in message
...
With the following code i keep getting this error. Can you help?
***Compile Error
***User_defined not type defined

Private Sub LoadMares()
Dim mares As New Scripting.Dictionary (THIS IS WHAT IS HIGHLIGHTED)
Dim index As Long
Dim Mare As String
For index = 2 To Source.Rows.Count
Mare = Source.Cells(index, 1)
If Not mares.Exists(Mare) Then
mares.Add Mare, Mare
cmbMare.AddItem Mare
End If
Next


End Sub
--
Though daily learning, I LOVE EXCEL!
Jennifer



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 266
Default Scripting.Dictionary question

"Bob Phillips" skrev i en meddelelse
...
Also it is bad practice to declare an object as New, it may get created
when
you least expect it, far better to us

Dim mares As Scripting.Dictionary

Set mares = New Scripting.Dictionary


--
HTH

Bob Phillips



Hi Bob

I don't understand your comment. If declared as New,
the object is *always* created the first time the
variable is used (as expected!?).
When is this bad?

Leo


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Scripting.Dictionary question

When you auto-instance a variable in this way, it is created the first time
that it is referenced, but that is WHENEVER it is referenced. When it is
encountered in code, it is checked to see if it is Nothing, and if so, an
instance of the object is created.

Thus, more code is generated and executed each time the variable is
encountered. Logically, the following train of events takes place
If myObject Is Nothing Then
Set myObject = New object_type
End If

This is no big deal in itself, but this actually precludes you from testing
for Nothing in your code.

Try this, create a class (no code) called clsTest, and run this code

Dim myObject As New clsTest

If myObject Is Nothing Then
MsgBox "empty"
End If

and this code doesn't error as it should

Set myObject = Nothing
myObject.myProp = "xyz"


In short, you lose control, and that is not good.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Leo Heuser" wrote in message
...
"Bob Phillips" skrev i en meddelelse
...
Also it is bad practice to declare an object as New, it may get created
when
you least expect it, far better to us

Dim mares As Scripting.Dictionary

Set mares = New Scripting.Dictionary


--
HTH

Bob Phillips



Hi Bob

I don't understand your comment. If declared as New,
the object is *always* created the first time the
variable is used (as expected!?).
When is this bad?

Leo






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Scripting.Dictionary question

Chip Pearson has some notes, too:
http://www.cpearson.com/excel/variables.htm
Look for: Don't Use The New Keyword In A Dim Statement


Leo Heuser wrote:

"Bob Phillips" skrev i en meddelelse
...
Also it is bad practice to declare an object as New, it may get created
when
you least expect it, far better to us

Dim mares As Scripting.Dictionary

Set mares = New Scripting.Dictionary


--
HTH

Bob Phillips


Hi Bob

I don't understand your comment. If declared as New,
the object is *always* created the first time the
variable is used (as expected!?).
When is this bad?

Leo


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 266
Default Scripting.Dictionary question

Thanks guys!

Leo


"Dave Peterson" skrev i en meddelelse
...
Chip Pearson has some notes, too:
http://www.cpearson.com/excel/variables.htm
Look for: Don't Use The New Keyword In A Dim Statement


Leo Heuser wrote:

"Bob Phillips" skrev i en meddelelse
...
Also it is bad practice to declare an object as New, it may get created
when
you least expect it, far better to us

Dim mares As Scripting.Dictionary

Set mares = New Scripting.Dictionary


--
HTH

Bob Phillips


Hi Bob

I don't understand your comment. If declared as New,
the object is *always* created the first time the
variable is used (as expected!?).
When is this bad?

Leo


--

Dave Peterson



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SCRIPTING APC25 Excel Programming 0 February 28th 06 07:31 PM
Scripting Question Becca Setting up and Configuration of Excel 2 September 22nd 05 12:33 AM
Newbie VBE scripting question jeff Excel Programming 1 December 20th 04 09:28 PM
VBA - running scripting dictionary (error!) ajliaks[_13_] Excel Programming 4 April 18th 04 10:13 PM
VBA question - Set reference to run Scripting.Dictionary ajliaks[_12_] Excel Programming 2 April 17th 04 03:47 PM


All times are GMT +1. The time now is 06:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"