Making a command button the EXACT size of a cell

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi there

Try this.
Stretch your command button so that it exactly coincides with the cell boundaries. Now right click the button, select Format Control, select the Properties tab, select Move and Size with cells. Now if you alter the width of your columns or rows the button will keep to the same size as the cell it occupies.

Hope this helps
Derek
 
Upvote 0
Hi,

Make sure you know what are the cell`s Width and Height. ( You can do this by right clicking on the far top or the far left of the Column/Row)

Right click on the command button, chose properties and in there you will have all informatio about the command button, including Size.

Good luck
 
Upvote 0
Hey Billy - -

Assuming this is a non-VBA question, you can "snap" any shape such as a Command Button in a cell by dragging one of its corner handles (example top left) while in Design mode and (here's the trick) pressing the Alt key. Then do the same thing with the opposite corner handle (example bottom right), again, while holding down the Alt key.

Release the Alt key, set the property for Move and Size with cells, and exit Design mode.


If this is a VBA question, then

If this is a Forms button you need to create and fit into cell C5 for example:

Code:
Sub CreateFormsButton()
Dim btn As Button
Dim rng As Range
With Worksheets("Sheet1")
Set rng = .Range("C5")
Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
With btn
.Caption = "Test"
.OnAction = "Test1"
End With
End With
End Sub


Else if this is an activex commandbutton you need to create and fit into C10 for example, and write event code for it at the same time:

Code:
Sub CreateCommandButton()
Dim ctop#, cleft#, cht#, cwdth#
Dim sht As Worksheet
Dim Btn As OLEObject
Set sht = ThisWorkbook.Worksheets("Sheet1")
With Range("C10")
ctop = .Top
cleft = .Left
cht = .Height
cwdth = .Width
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 = "Click Me"
Btn.Name = "MyButton"
Btn.Placement = xlMoveAndSize
'Optional code insertion - - establish ref in VBE to MS VBA Extensibility 5.3 Library
With ThisWorkbook.VBProject.VBComponents(sht.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", Btn.Name) + 1, "Msgbox ""Replace this message with your actual code."" "
End With
End Sub
 
Upvote 0
Hi Tom - I've never seen a variable declaration with written with a # at the end. What does that mean?
Dim ctop#, cleft#, cht#, cwdth#
 
Upvote 0
Hi Tom - I've never seen a variable declaration with written with a # at the end. What does that mean?
Dim ctop#, cleft#, cht#, cwdth#


The hashtag means double.

 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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