Adding control buttons to a list with VBA- Need Help

mdi

New Member
Joined
Jul 18, 2011
Messages
5
I am trying to go through a list of cells and add buttons to adjacent cells if they meet certain criteria. As a practice to get down the basic algorithm i wrote this code.

Option Explicit
Sub Create_Command_Buttons()
Dim currentCell As Range
Dim buttonArray(114) As OLEObject
Dim i As Integer
i = 1
currentCell = Range("J1")
Do While i <= 114
If currentCell.Offset(0, -2) = "" Then
Set buttonArray(i) = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=currentCell.Left, Top:=currentCell.Top, Height:=30, Width:=120)
End If
i = i + 1
currentCell = currentCell.Offset(1, 0)
Loop
End Sub

When I run it I get an error: Object variable or With block not set

Any information on how I could get this to compile would be greatly appreciated.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Code:
Sub Create_Command_Buttons()
    Dim currentCell As Range
    Dim buttonArray(114) As OLEObject
    Dim i      As Integer
    i = 1
    [COLOR="Red"]Set [/COLOR]currentCell = Range("J1")
    Do While i <= 114
        If currentCell.Offset(0, -2) = "" Then
            Set buttonArray(i) = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=currentCell.Left, Top:=currentCell.Top, Height:=30, Width:=120)
        End If
        i = i + 1
        [COLOR="Red"]Set[/COLOR] currentCell = currentCell.Offset(1, 0)
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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