Pin Command Button to Cell

Rocksie

New Member
Joined
May 17, 2017
Messages
26
Hi All,

I have the following code to pin a command button to a specific cell. My question is how to modify this code to pin different command buttons to different cells. The code is written in the workbook activate module, so that when I open the workbook the buttons are in the correct places.

Dim rng As Range
Set rng = ActiveSheet.Range("B3")
With ActiveSheet.OLEObjects("CommandButton1")
.Top = rng.Top
.Left = rng.Left
.Width = rng.Width
.Height = rng.RowHeight
End With


Thanks for your advice
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Like this:
Code:
Private Sub Worksheet_Activate()
Dim rng As Range
With ActiveSheet.OLEObjects("CommandButton1")
Set rng = ActiveSheet.Range("B3")
.Top = rng.Top
.Left = rng.Left
.Width = rng.Width
.Height = rng.RowHeight
End With

With ActiveSheet.OLEObjects("CommandButton2")
Set rng = ActiveSheet.Range("J3")
.Top = rng.Top
.Left = rng.Left
.Width = rng.Width
.Height = rng.RowHeight
End With
End Sub
 
Upvote 0
Ok, so I have to do all 30 command buttons separately.... I thought there could be an easier and quicker way...
 
Upvote 0
Ok, so I have to do all 30 command buttons separately.... I thought there could be an easier and quicker way...

You did not say you had 30.

Give me some relationship to work with.

For example button 1 to B1
Button 2 to B2
Button 3 to B3

etc.

Tell me more about what your doing.
Maybe I can provide another way without needing 30 command buttons.
Which will require 30 different scripts
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,038
Latest member
apwr

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