Create a collection of controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Office 2003 on Windows XP.

I have several controls on a spreadsheet (non-ActiveX controls) and I want
to add these objects to a collection. How do I do this? What I have so far
follows, but ofcourse generates an error:

Dim colObject As New Collection
Dim vItem As Variant
Dim oItems As Controls
Dim lX As Long

Set oItems = ActiveSheet.Controls
For Each vItem In oItems
lX = lX + 1
colObject.Add vItem, lX
Next

Could someone please correct my code or supply example code on how to do
this? Thanks much in advance.
 
Quartz,

Try the following code

Dim colObject As New Collection
Dim vItem As Variant
Dim oItems As OLEObjects
Dim lX As Long

Set oItems = ActiveSheet.OLEObjects
For Each vItem In oItems
lX = lX + 1
colObject.Add vItem, Format(lX, "0")
Next


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Thanks Chip!
Shouldn't I be able to obtain a count of items in the collection using:

MsgBox colObject.Count

In a test there are three controls in the sheet, but the count returns zero...
 
Quartz,

Debug.Print colObject.Count

returns the correct count for me. Step through your code to
ensure that the items are indeed added to the collection.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Chip,
The items are NOT being added. I am using the exact code, what am I doing
wrong? Do I need a library reference?

I very much appreciate your assistance...
 
I can't think of a reason that they are not being added. Do you
have an On Error Resume Next in your procedure. If you do,
comment it out so you can see the run time error (if any) that is
causing the error. The only thing I can think of is that you are
attempting to add an item to the collection and the key value
already exists in the collection.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Ah, I see, I added a few ActiveX controls to the sheet and it works fine.
Actually, I'm using Non-ActiveX controls... how would I do that?
 
Chip,
The objects in question work if I dim them as Shapes rather than OLEObject,
but then, I get the ActiveX objects as well. I only want the NON-activeX
items.

Sorry, I have to leave now. I will repost tomorrow if I can't crack it.

Thanks again for your help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top