PC Review


Reply
Thread Tools Rate Thread

Collection::Item runtime error

 
 
PaulH
Guest
Posts: n/a
 
      27th Aug 2007
I have an excel vba macro that uses a collection to store some string
data. The collection 'key' is a string that is shown in a listbox. (as
below)

When accessing the 'Item' property, I get the runtime error: "Object
variable or With block variable not set"

' lbFiles is a listbox
Dim FileNames_ As Collection
Private Sub btn_Click()
Dim ListItem As Integer
ListItem = GetSelected()

If ListItem > -1 Then
Dim Path As String
Path = FileNames_.item( lbFiles.List(ListItem)) 'error here
'...
End If

'...
End Sub

I noticed that in all the documentation, the 'Item' property is upper
case, but if I try to type 'FileNames_.Item' in to vba, it
automatically lowers the case. (as above) Does that make a difference?

Thanks,
Paul

 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      27th Aug 2007

You need a Set statement to create the Collection...
Set FileNames_ = New Collection.

Also, the first item in a Collection has an index of 1,
while the first item in a Listbox has an index of 0.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"PaulH" <(E-Mail Removed)>
wrote in message
I have an excel vba macro that uses a collection to store some string
data. The collection 'key' is a string that is shown in a listbox. (as
below)
When accessing the 'Item' property, I get the runtime error: "Object
variable or With block variable not set"

' lbFiles is a listbox
Dim FileNames_ As Collection
Private Sub btn_Click()
Dim ListItem As Integer
ListItem = GetSelected()

If ListItem > -1 Then
Dim Path As String
Path = FileNames_.item( lbFiles.List(ListItem)) 'error here
'...
End If

'...
End Sub
I noticed that in all the documentation, the 'Item' property is upper
case, but if I try to type 'FileNames_.Item' in to vba, it
automatically lowers the case. (as above) Does that make a difference?
Thanks,
Paul

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      27th Aug 2007
A couple of things to try...

Add something like this prior to the line that causes the problem
msgbox ListItem
msgbox lbFiles.List(ListItem)
msgbox FileNames_.item(1)

Your problem with "item" is probably that you have declared item as a
Variable somewhere (either intentionally or unintentionally). Do you use
option explicit in your code? I noticed that you are not using variable
prefixes like strFileNames. That is an easy way of making sure that your
variables do not replace the reserved words.

--
HTH...

Jim Thomlinson


"PaulH" wrote:

> I have an excel vba macro that uses a collection to store some string
> data. The collection 'key' is a string that is shown in a listbox. (as
> below)
>
> When accessing the 'Item' property, I get the runtime error: "Object
> variable or With block variable not set"
>
> ' lbFiles is a listbox
> Dim FileNames_ As Collection
> Private Sub btn_Click()
> Dim ListItem As Integer
> ListItem = GetSelected()
>
> If ListItem > -1 Then
> Dim Path As String
> Path = FileNames_.item( lbFiles.List(ListItem)) 'error here
> '...
> End If
>
> '...
> End Sub
>
> I noticed that in all the documentation, the 'Item' property is upper
> case, but if I try to type 'FileNames_.Item' in to vba, it
> automatically lowers the case. (as above) Does that make a difference?
>
> Thanks,
> Paul
>
>

 
Reply With Quote
 
PaulH
Guest
Posts: n/a
 
      27th Aug 2007
On Aug 27, 1:45 pm, "Jim Cone" <jim.cone...@rcn.comXXX> wrote:
> You need a Set statement to create the Collection...
> Set FileNames_ = New Collection.
>
> Also, the first item in a Collection has an index of 1,
> while the first item in a Listbox has an index of 0.
> --
> Jim Cone
> San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
> (Excel Add-ins / Excel Programming)
>
> "PaulH" <paul.h...@gmail.com>
> wrote in message
> I have an excel vba macro that uses a collection to store some string
> data. The collection 'key' is a string that is shown in a listbox. (as
> below)
> When accessing the 'Item' property, I get the runtime error: "Object
> variable or With block variable not set"
>
> ' lbFiles is a listbox
> Dim FileNames_ As Collection
> Private Sub btn_Click()
> Dim ListItem As Integer
> ListItem = GetSelected()
>
> If ListItem > -1 Then
> Dim Path As String
> Path = FileNames_.item( lbFiles.List(ListItem)) 'error here
> '...
> End If
>
> '...
> End Sub
> I noticed that in all the documentation, the 'Item' property is upper
> case, but if I try to type 'FileNames_.Item' in to vba, it
> automatically lowers the case. (as above) Does that make a difference?
> Thanks,
> Paul


Ah, the 'new' keyword does it!

What does the 'set' keyword do? Does it replace the 'Dim'?
Is there a 'delete' (or something equivalent) I need to do since I'm
using 'new'?

Thanks,
PaulH

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      27th Aug 2007
Check out this link...

http://www.cpearson.com/excel/variables.htm

Set is used with objects when you want to create, modfy or destroy the
object. (You can change the properties of an existing object without set)
--
HTH...

Jim Thomlinson


"PaulH" wrote:

> On Aug 27, 1:45 pm, "Jim Cone" <jim.cone...@rcn.comXXX> wrote:
> > You need a Set statement to create the Collection...
> > Set FileNames_ = New Collection.
> >
> > Also, the first item in a Collection has an index of 1,
> > while the first item in a Listbox has an index of 0.
> > --
> > Jim Cone
> > San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
> > (Excel Add-ins / Excel Programming)
> >
> > "PaulH" <paul.h...@gmail.com>
> > wrote in message
> > I have an excel vba macro that uses a collection to store some string
> > data. The collection 'key' is a string that is shown in a listbox. (as
> > below)
> > When accessing the 'Item' property, I get the runtime error: "Object
> > variable or With block variable not set"
> >
> > ' lbFiles is a listbox
> > Dim FileNames_ As Collection
> > Private Sub btn_Click()
> > Dim ListItem As Integer
> > ListItem = GetSelected()
> >
> > If ListItem > -1 Then
> > Dim Path As String
> > Path = FileNames_.item( lbFiles.List(ListItem)) 'error here
> > '...
> > End If
> >
> > '...
> > End Sub
> > I noticed that in all the documentation, the 'Item' property is upper
> > case, but if I try to type 'FileNames_.Item' in to vba, it
> > automatically lowers the case. (as above) Does that make a difference?
> > Thanks,
> > Paul

>
> Ah, the 'new' keyword does it!
>
> What does the 'set' keyword do? Does it replace the 'Dim'?
> Is there a 'delete' (or something equivalent) I need to do since I'm
> using 'new'?
>
> Thanks,
> PaulH
>
>

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      27th Aug 2007

A collection is an intrinsic VBA object.
The Set statement assigns a new instance of the object to your variable.
Dim tells Excel what variable you will be using and to allocate some memory.
Both Dim and Set are required.
(note: they can be combined into one statement, but that is bad programming practice)

At the point in your code that the collection will not be used or referred to
again, you can free up memory by using...
Set FileNames_ = Nothing.

Some useful advice here...
http://www.cpearson.com/excel/newposte.htm
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)




"PaulH"
<(E-Mail Removed)>
wrote in message
On Aug 27, 1:45 pm, "Jim Cone"
wrote:
> You need a Set statement to create the Collection...
> Set FileNames_ = New Collection.
>
> Also, the first item in a Collection has an index of 1,
> while the first item in a Listbox has an index of 0.
> --
> Jim Cone
> San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
> (Excel Add-ins / Excel Programming)
>
> "PaulH" <paul.h...@gmail.com>
> wrote in message


Ah, the 'new' keyword does it!
What does the 'set' keyword do? Does it replace the 'Dim'?
Is there a 'delete' (or something equivalent) I need to do since I'm
using 'new'?
Thanks,
PaulH

 
Reply With Quote
 
PaulH
Guest
Posts: n/a
 
      27th Aug 2007
On Aug 27, 2:48 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com> wrote:
> Check out this link...
>
> http://www.cpearson.com/excel/variables.htm
>
> Set is used with objects when you want to create, modfy or destroy the
> object. (You can change the properties of an existing object without set)
> --
> HTH...
>
> Jim Thomlinson
>
> "PaulH" wrote:
> > On Aug 27, 1:45 pm, "Jim Cone" <jim.cone...@rcn.comXXX> wrote:
> > > You need a Set statement to create the Collection...
> > > Set FileNames_ = New Collection.

>
> > > Also, the first item in a Collection has an index of 1,
> > > while the first item in a Listbox has an index of 0.
> > > --
> > > Jim Cone
> > > San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
> > > (Excel Add-ins / Excel Programming)

>
> > > "PaulH" <paul.h...@gmail.com>
> > > wrote in message
> > > I have an excel vba macro that uses a collection to store some string
> > > data. The collection 'key' is a string that is shown in a listbox. (as
> > > below)
> > > When accessing the 'Item' property, I get the runtime error: "Object
> > > variable or With block variable not set"

>
> > > ' lbFiles is a listbox
> > > Dim FileNames_ As Collection
> > > Private Sub btn_Click()
> > > Dim ListItem As Integer
> > > ListItem = GetSelected()

>
> > > If ListItem > -1 Then
> > > Dim Path As String
> > > Path = FileNames_.item( lbFiles.List(ListItem)) 'error here
> > > '...
> > > End If

>
> > > '...
> > > End Sub
> > > I noticed that in all the documentation, the 'Item' property is upper
> > > case, but if I try to type 'FileNames_.Item' in to vba, it
> > > automatically lowers the case. (as above) Does that make a difference?
> > > Thanks,
> > > Paul

>
> > Ah, the 'new' keyword does it!

>
> > What does the 'set' keyword do? Does it replace the 'Dim'?
> > Is there a 'delete' (or something equivalent) I need to do since I'm
> > using 'new'?

>
> > Thanks,
> > PaulH


Those are both really useful pages. Thanks, guys.

-PaulH

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Item not found in this collection error 8020 Microsoft Access VBA Modules 1 3rd Nov 2009 02:52 PM
runtime error 3265 item not found in this collection =?Utf-8?B?SmFuaXM=?= Microsoft Access VBA Modules 1 22nd Aug 2006 08:51 PM
Item not found in this collection error =?Utf-8?B?QnJhbmRvbg==?= Microsoft Access 6 9th Nov 2005 07:21 PM
Error : Item not found in this collection spowell15@yahoo.co.uk Microsoft Access Queries 7 24th Jun 2005 05:23 PM
error 3265 Item not in this collection? mikebres Microsoft Access VBA Modules 2 29th Mar 2004 10:00 PM


Features
 

Advertising
 

Newsgroups
 


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