I am in the process of adding command buttons to a worksheet but am having trouble changing a specific buttons properties correctly.
Private Sub RoundedRectangle3_Click()
Dim temp As String
Dim intCounter As Integer
MyArray = Array("A", "B", "C", "D", "C", "E", "F", "G", "H", "I", "J", "K")
For intCounter = 0 To 5
temp = MyArray(intCounter) & 1
CreateCommandButtonLeft (temp)
CreateCommandButtonRight (temp)
Next
End Sub
Function CreateCommandButtonLeft(temp)
Dim ctop#, cleft#, cht#, cwdth#, sht As Worksheet, Btn As OLEObject
Set sht = ThisWorkbook.Worksheets("Sheet1")
With Range(temp)
ctop = .Top
cleft = .Left
cht = .Height
cwdth = .Width / 2
End With
With sht
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=cleft, Top:=ctop, Width:=cwdth, Height:=cht)
'Btn.Select
End With
Btn.Object.Caption = "ç"
Btn.Name = "MyButtonL" & temp
Selection.Font.Name = "Wingdings"
Btn.Placement = xlMoveAndSize
End Function
Function CreateCommandButtonRight(temp)
Dim ctop#, cleft#, cht#, cwdth#, sht As Worksheet, Btn As OLEObject
Set sht = ThisWorkbook.Worksheets("Sheet1")
With Range(temp)
ctop = .Top
cleft = .Left + (.Width / 2)
cht = .Height
cwdth = .Width / 2
End With
With sht
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=cleft, Top:=ctop, Width:=cwdth, Height:=cht)
End With
Btn.Object.Caption = "è"
Selection.Font.Name = "Wingdings"
Btn.Name = "MyButtonR" & temp
Btn.Placement = xlMoveAndSize
End Function
Sub RoundedRectangle3_Click basically decides where I will put a button.
For intCounter = 0 To 5
temp = MyArray(intCounter) & 1
The '& 1' at the end of those lines is how I decide which row to put the 5 buttons in.
From the same For-Next loop I call both of the functions that will create commandbuttons. This is when I start getting problems. I run the code in row 1 and the buttons are created perfectly except column 'C' which places 2 buttons on top of each other. (If you delete one button another is under it.)
The code in these functions is not my own which is why I don't fully understand but am hoping someone can explain what is going on.
After running the code in row 1 I attempt to run the code in row 2 or 3 and am able to do so but the font is wrong. (Not wingdings.)
Finally, every once in a while I will get an error:
'Object library invalid or contains references to object definitions that could not be found.'
If anyone can explain to me why the code does this I should be able to figure the rest out. I am still learning the syntax of VBA.
Thanks for any help.
EDIT: In the following line why does it have to be "Forms.CommandButton.1"
With sht
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=cleft, Top:=ctop, Width:=cwdth, Height:=cht)
'Btn.Select
End With
Bookmarks