NigelTufnel
Board Regular
- Joined
- Apr 3, 2008
- Messages
- 53
- Office Version
- 365
- Platform
- Windows
Apologies in advance for what will be a long post, but I've tried a lot of things and I want to be as clear as possible in explaining my problem to maximize the likelihood that someone can help.
To sum up my problem, I can place an ActiveX control on my worksheet, but I cannot seem to edit its properties as I desire. My head is about to explode after hours of trying to understand OLEobjects and ControlFormat properties.
I am a novice VBA programmer with years of VBA programming, yet with little experience dealing with ActiveX controls. I am using Excel 2007 in Windows 7.
I am trying to place a multi-select listbox control (including checkboxes) on an Excel worksheet programmatically. I can easily place this control and format it to my heart's content from the Developer Menu in the Excel 2007 Ribbon interface, but I need to be able to do this from within VBA. (The user of my application will be creating these list boxes by pressing a button.)
I'd prefer to use the simpler Forms ListBox control, but from what I can tell, it doesn't seem to allow the option to place checkboxes next to the list items, which is a feature my users would find very helpful.
OK, so I recorded a macro in which I placed the ListBox ActiveX control on my worksheet and set the properties using the properties dialog box. The first thing I noticed was that the control properties I selected during the session were not recorded in the macro. Bummer. I then modified the code to produce the following snippet:
The ListBox was successfully filled with the desired items. So far, so good.
Then I added the following line...and it gave me an error:
I get the dreaded "Object does not support this property or method". I didn't expect this. All I'm doing is setting the property to allow multiple selections.
So, problem #1 is...how do I get access to all of the properties shown for the List Box in the Properties Window?
The next problem came when I tried to give the ListBox a specific name. I replaced the code above with the following:
This code, when executed, resulted in a blank list box, and an error: "Object does not support this property or method." Even if I replace the text "ListBoxName" with the index number of the List Box in the Shapes collection, I still get an error.
OK, so I did more research and tried to set the properties using the following syntax:
This also gave me an "Object does not support this property or method." error from VBA.
But then I copied these three lines verbatim to the VBE Immediate Window and they all executed to perfection when I tried them one at a time. Why they work in the immediate window and not as programmed code, I have no idea.
Of course, even if I get that syntax to work in my code, it doesn't solve my problem as the List Box name "Temp" is a constant in these expressions, whereas I want to be able to refer to the list box using a variable (as I will have multiple list boxes on the same worksheet).
At this point, I am completely frustrated and out of my league. I clearly lack some basic understanding of ActiveX controls and how they are manipulated using VBA syntax.
Can someone help me here? I'd settle for knowing how to code this correctly, but if someone can explain (or give me a reference to) how to understand ActiveX controls on worksheets (as opposed to forms) I'd be most appreciative.
Thanks for your patience.
To sum up my problem, I can place an ActiveX control on my worksheet, but I cannot seem to edit its properties as I desire. My head is about to explode after hours of trying to understand OLEobjects and ControlFormat properties.
I am a novice VBA programmer with years of VBA programming, yet with little experience dealing with ActiveX controls. I am using Excel 2007 in Windows 7.
I am trying to place a multi-select listbox control (including checkboxes) on an Excel worksheet programmatically. I can easily place this control and format it to my heart's content from the Developer Menu in the Excel 2007 Ribbon interface, but I need to be able to do this from within VBA. (The user of my application will be creating these list boxes by pressing a button.)
I'd prefer to use the simpler Forms ListBox control, but from what I can tell, it doesn't seem to allow the option to place checkboxes next to the list items, which is a feature my users would find very helpful.
OK, so I recorded a macro in which I placed the ListBox ActiveX control on my worksheet and set the properties using the properties dialog box. The first thing I noticed was that the control properties I selected during the session were not recorded in the macro. Bummer. I then modified the code to produce the following snippet:
PHP:
ActiveSheet.OLEObjects.Add(ClassType:="Forms.ListBox.1", Link:=False, _
DisplayAsIcon:=False, Left:=369, Top:=174, Width:=106.8, Height:=54.6 _
).Select
Selection.ListFillRange = "MyRange"
Then I added the following line...and it gave me an error:
PHP:
Selection.MultiSelect = fmMultiSelectMulti
So, problem #1 is...how do I get access to all of the properties shown for the List Box in the Properties Window?
The next problem came when I tried to give the ListBox a specific name. I replaced the code above with the following:
PHP:
ListBoxName = "Temp"
ActiveSheet.OLEObjects.Add(ClassType:="Forms.ListBox.1", Link:=False, _
DisplayAsIcon:=False, Left:=369, Top:=174, Width:=106.8, Height:=54.6 _
).Name = ListBoxName
ActiveSheet.Shapes(ListBoxName).ControlFormat.ListFillRange = "MyRange"
OK, so I did more research and tried to set the properties using the following syntax:
PHP:
ActiveSheet.Temp.ListFillRange="MyRange"
Activesheet.Temp.MultiSelect=fmMultiSelectMulti
Activesheet.Temp.ListStyle=fmListStyleOption
But then I copied these three lines verbatim to the VBE Immediate Window and they all executed to perfection when I tried them one at a time. Why they work in the immediate window and not as programmed code, I have no idea.
Of course, even if I get that syntax to work in my code, it doesn't solve my problem as the List Box name "Temp" is a constant in these expressions, whereas I want to be able to refer to the list box using a variable (as I will have multiple list boxes on the same worksheet).
At this point, I am completely frustrated and out of my league. I clearly lack some basic understanding of ActiveX controls and how they are manipulated using VBA syntax.
Can someone help me here? I'd settle for knowing how to code this correctly, but if someone can explain (or give me a reference to) how to understand ActiveX controls on worksheets (as opposed to forms) I'd be most appreciative.
Thanks for your patience.