Macro or code to copy a "command button"

johannordgren

New Member
Joined
Apr 24, 2018
Messages
31
As the title says, my need is to copy a made command button to another 500 rows.

The code I'm using is this;

Private Sub CommandButton1_Click()
Dim x As Integer
x = Range("A1").Value
Range("A1").Value = x + 1
End Sub


Which makes a count of how many times the button is clicked.

What i would like is to copy this button, pretty much like you would copa down a normal excel cell. I'm talking about the "draging down".

Essentialy i would like the cells in the formula above (A1) to automaticly change, or if you are smarter than me, which you probably are, make a completely different code.

I dont even know if this is possible, but if it is, please help!
 
Now if you insist on having buttons in column B then use this script

This is a Module script. Put this script in your workbook.
Code:
Sub new_vergraph()
'Modified 4-24-18 10:05 AM EDT
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Btnrng.Offset(0, 1).Value = Btnrng.Offset(0, 1).Value + 1
End Sub
The above script just sets in your workbook it runs automatically when you click on one of your 50 buttons

Then put this script in your workbook. And this script will install buttons in rows 1 to 50 of column B
Change 50 if you want more then 50
Code:
Sub My_Button()
'Modified 4-24-18 10:05 AM EDT
Dim Lastrow As Long
Lastrow = 50  ' Modify 50 to the number of rows you want buttons in.
Dim x As Long
x = 0
Dim r As Range
    For Each r In Range("B1:B" & Lastrow)
        x = x + 1
        With r.Parent.Buttons.Add(r.Left, r.Top, r.Width, r.Height)
                    .Font.Bold = True
                    .Caption = x
                    .OnAction = "new_vergraph" & numb
    
                End With
        Next r
End Sub



When you click on one of these buttons your script will do what you want.
 
Last edited:
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
We can always add more if you wanted.
Like this script will do the same as the previous one but will put today date in column A same row.
The date is static and will not change tomorrow:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Modified 4-25-18 9:15 AM EDT
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Cancel = True
Target.Offset(, 1).Value = Target.Offset(, 1).Value + 1
Target.Offset(, -1).Value = Date
End If
End Sub
 
Upvote 0
Actually! I just discovered an issue..

I would like to be able to filter the file. Though now it is not possible to filter column A and B (which I use the script for).

Is this something that can be resolved? Or it doesn't work that way?
 
Upvote 0
I installed a Table in this range and was able to filter.

If you have a script your using to filter this range then show me the script.

Or define filter if your not talking about a Table Filter.
 
Upvote 0
I never use filtering that way.

Not sure how my double click script could be causing a filter problem.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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