+ Reply to Thread
Results 1 to 8 of 8

Create a collection of controls

  1. #1
    quartz
    Guest

    Create a collection of controls

    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.

  2. #2
    Chip Pearson
    Guest

    Re: Create a collection of controls

    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



    "quartz" <[email protected]> wrote in message
    news:[email protected]...
    >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.




  3. #3
    quartz
    Guest

    Re: Create a collection of controls

    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...


    "Chip Pearson" wrote:

    > 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
    >
    >
    >
    > "quartz" <[email protected]> wrote in message
    > news:[email protected]...
    > >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.

    >
    >
    >


  4. #4
    Chip Pearson
    Guest

    Re: Create a collection of controls

    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



    "quartz" <[email protected]> wrote in message
    news:[email protected]...
    > 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...
    >
    >
    > "Chip Pearson" wrote:
    >
    >> 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
    >>
    >>
    >>
    >> "quartz" <[email protected]> wrote in message
    >> news:[email protected]...
    >> >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.

    >>
    >>
    >>




  5. #5
    quartz
    Guest

    Re: Create a collection of controls

    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...

    "Chip Pearson" wrote:

    > 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
    >
    >
    >
    > "quartz" <[email protected]> wrote in message
    > news:[email protected]...
    > > 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...
    > >
    > >
    > > "Chip Pearson" wrote:
    > >
    > >> 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
    > >>
    > >>
    > >>
    > >> "quartz" <[email protected]> wrote in message
    > >> news:[email protected]...
    > >> >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.
    > >>
    > >>
    > >>

    >
    >
    >


  6. #6
    Chip Pearson
    Guest

    Re: Create a collection of controls

    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


    "quartz" <[email protected]> wrote in message
    news:[email protected]...
    > 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...
    >
    > "Chip Pearson" wrote:
    >
    >> 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
    >>
    >>
    >>
    >> "quartz" <[email protected]> wrote in message
    >> news:[email protected]...
    >> > 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...
    >> >
    >> >
    >> > "Chip Pearson" wrote:
    >> >
    >> >> 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
    >> >>
    >> >>
    >> >>
    >> >> "quartz" <[email protected]> wrote in
    >> >> message
    >> >> news:[email protected]...
    >> >> >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.
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>




  7. #7
    quartz
    Guest

    Re: Create a collection of controls

    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?

    "quartz" wrote:

    > 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...
    >
    > "Chip Pearson" wrote:
    >
    > > 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
    > >
    > >
    > >
    > > "quartz" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > 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...
    > > >
    > > >
    > > > "Chip Pearson" wrote:
    > > >
    > > >> 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
    > > >>
    > > >>
    > > >>
    > > >> "quartz" <[email protected]> wrote in message
    > > >> news:[email protected]...
    > > >> >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.
    > > >>
    > > >>
    > > >>

    > >
    > >
    > >


  8. #8
    quartz
    Guest

    Re: Create a collection of controls

    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!!!

    "Chip Pearson" wrote:

    > 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
    >
    >
    > "quartz" <[email protected]> wrote in message
    > news:[email protected]...
    > > 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...
    > >
    > > "Chip Pearson" wrote:
    > >
    > >> 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
    > >>
    > >>
    > >>
    > >> "quartz" <[email protected]> wrote in message
    > >> news:[email protected]...
    > >> > 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...
    > >> >
    > >> >
    > >> > "Chip Pearson" wrote:
    > >> >
    > >> >> 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
    > >> >>
    > >> >>
    > >> >>
    > >> >> "quartz" <[email protected]> wrote in
    > >> >> message
    > >> >> news:[email protected]...
    > >> >> >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.
    > >> >>
    > >> >>
    > >> >>
    > >>
    > >>
    > >>

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1