control arrays excel 2003

sbnktn

New Member
Joined
Jul 22, 2013
Messages
24
i use excel 2003. I have written following code in 'this workbook' as it has to run when the workbook is opened.
Code:
Dim ctrlarray(1 To 5) As Excel.OLEObject
Dim i As Integer
For i = 1 To 5
Set ctrlarray(i) = ActiveSheet.OLEObjects.Add("Commandbutton1")
Next i
but for some reason it gives error on line with 'Set'. I need help to set this right.
Thanks!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The class type is wrong - try:
Code:
Set ctrlarray(i) = ActiveSheet.OLEObjects.Add("Forms.Commandbutton.1")
 
Upvote 0
thanks for the response. its working but adds only one command button wherein i require five. what might be wrong?
 
Upvote 0
I think you'll find you have 5 - they just happen to be positioned on top of one another because you didn't specify where they were to go. ;)
 
Upvote 0
can i reposition the buttons within the array and also give different names. I tried formatting using with....statement within the loop. but it was not helpful. how do i write the code for different left, top position and name being different for these 5 buttons. hope i am not asking more !! :-)
 
Upvote 0
You can use something like this, for example:
Code:
Dim ctrlarray(1 To 5) As Excel.OLEObject
Dim i As Integer
For i = 1 To 5
   Set ctrlarray(i) = ActiveSheet.OLEObjects.Add(classtype:="Forms.Commandbutton.1", Left:=0, Top:=(i - 1) * 25, Width:=50, Height:=25)
   ctrlarray(i).Name = "Button" & i
Next i
 
Upvote 0
hey that really works! can u explain me the details used in Top:= (i-1)*25. i understand 25 is the space between each boxes right! how to specify the space from the top to the first button?
 
Upvote 0
For the first button, i = 1 so (i-1)*25 = 0 * 25
If you want to move them all down, simply add a constant value - for example:
Code:
Top:= (i-1)*25 + 50
 
Upvote 0
thanks a ton! that really worked. I would like to correct myself when i told about the different names i ctrlarray(i).Name = "Button" & i. i meant working with captions here. I think i have to use text variable here right!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top