create button using vba

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
Code:
Dim button As OLEObject
    With ActiveCell
        Set button = ActiveSheet.OLEObjects.Add _
            (ClassType:="Forms.CommandButton.1", _
            Link:=False, DisplayAsIcon:=False, _
            Left:=.Left, _
            Top:=.Top, _
            Height:=.Height, _
            Width:=.Width)
 
        button.Object.Caption = .Row
        button.Object.Font.Size = 6
        button.FontStyle = "Bold"
        button.Object.WordWrap = True
        button.Name = "line" & .Row
    End With
I wrote this code and executed. It always shows an error message: "Object doesn't support this property or method". The button was created though error, but the fonstyle was not set to be bold.
I also want to set the button property to be "don't move or size with cells", what should I do in vba?

Best regards,
lolo^^
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I found it's the error takes place at :

Code:
button.Object.Font.FontStyle = "Bold"

how should I set this property?
 
Upvote 0
Soloved!
Code:
Dim button As OLEObject
    With ActiveCell
        Set button = ActiveSheet.OLEObjects.Add _
            (ClassType:="Forms.CommandButton.1", _
            Link:=False, DisplayAsIcon:=False, _
            Left:=.Left, _
            Top:=.Top, _
            Height:=.Height, _
            Width:=.Width)
 
        button.Object.Caption = .Row
        button.Object.Font.Size = 6
        button.Object.Font.Bold = True
        button.Object.WordWrap = True
        button.Name = "line" & .Row
    End With
Code:
Dim button As OLEObject
    With ActiveCell
        Set button = ActiveSheet.OLEObjects.Add _
            (ClassType:="Forms.CommandButton.1", _
            Link:=False, DisplayAsIcon:=False, _
            Left:=.Left, _
            Top:=.Top, _
            Height:=.Height, _
            Width:=.Width)
 
        button.Object.Caption = .Row
        button.Object.Font.Size = 6
        button.FontStyle = "Bold"
        button.Object.Font.Name = "MS Gothic"  
        button.Object.WordWrap = True
        button.Name = "line" & .Row
    End With
I wrote this code and executed. It always shows an error message: "Object doesn't support this property or method". The button was created though error, but the fonstyle was not set to be bold.
I also want to set the button property to be "don't move or size with cells", what should I do in vba?

Best regards,
lolo^^
 
Last edited:
Upvote 0
Thank you so much for posting the solution to this.

I was challenged with the same kind of issue (wanted to toggle button font from "bold" to "regular" on click) and tried everything I could think of... Didn't think of ".Font.Bold = " (though I had several variations on the theme that were close, hadn't landed on the right one :-)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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