CommandButton Question?

mayoung

Active Member
Joined
Mar 26, 2014
Messages
257
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
How can I modify this code so it works with Range(“C10:C11”) or if I wanted it to work with Range(“C10:D11”) instead of just Range(“C10”)? Thanks in advance!

VBA Code:
Sub CreateCommandButton1()
 
Application.ScreenUpdating = False
 
Dim cBut As OLEObject
Dim ctop#, cleft#, cht#, cwdth#
Dim sht As Worksheet
Dim Btn As OLEObject
On Error Resume Next
Set sht = ThisWorkbook.Worksheets("Sheet1")
Range("C10").Select
With ActiveSheet
.Shapes("MyButton").Delete
End With
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 = "Format 2 Page Quote"
Btn.Name = "MyButton"
Btn.Object.Font = "Verdana"
Btn.Object.Font.Bold = True
Btn.Object.Font.Size = 10
' Btn.Object.BackColor = &HFF&
Btn.Object.BackColor = &HFF8080
Btn.Object.ForeColor = &HFFFFFF
Btn.Placement = xlMoveAndSize
 
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Just change the With - End With range you use to set the button's position variables:
VBA Code:
With Range("C10:D11")
ctop = .Top
cleft = .Left
cht = .Height
cwdth = .Width
End With
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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