[VBA] Macro to create buttons with dynamic names (name +1)

Trikson

Board Regular
Joined
Feb 10, 2014
Messages
61
Hi guys,

I have a code that pastes some data in the next free row and creates formcontrol button in the cells next to this data:

Rich (BB code):
Sub

Dim NextRow As RangeDim NextComment As Range
Dim i As Integer


Set NextRow = Sheets(Me.ProjectName_Box.Text).Cells(Rows.Count, 2).End(xlUp).Offset(6, 0)
Set NextComment = NextRow.Offset(0, 5)


Sheets("Template").Select
Range("B5:E11").Select
Selection.Copy
Sheets(Me.ProjectName_Box.Text).Select
NextRow.PasteSpecial Paste:=xlPasteAll




ActiveSheet.Buttons.Add(NextComment.Left, NextComment.Top, 81, 14.25).Select
Selection.Caption = "Comment"
For i = 1 To 50
Selection.Name = "Comment" & i
Next i
Selection.OnAction = "CommentButton_Click"

When run, macro creates the Comment button named Comment50. When run again, the data is pasted correctly below the last entry, button is also created in the correct place, but its named Comment50 again (instead of Comment49).

The naming is important because of the "CommentButton_Click" macro that hides/unhides rows:

Rich (BB code):
Sub CommentButton_Click()


    Dim Btn As Button
    
    Set Btn = ActiveSheet.Buttons(Application.Caller)
    
    With Btn.TopLeftCell.Offset(1, 0).Resize(5).EntireRow
        .Hidden = Not .Hidden
    End With


End Sub

When both buttons are named Comment50 then clicking on either of them will hide rows that are below the topmost Comment50 button.

Appreciate any help/suggestions.
Thanks in advace!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Why do you have this code?
Code:
For i = 1 To 50
Selection.Name = "Comment" & i
Next i
Isn't that always going to name the newly created button 'Comment50'?

Couldn't you use code to find the name of the last button added and use that to name the next button?
Code:
strLastBtnName = ActiveSheet.Buttons(ActiveSheet.Buttons.Count).Name

strNewBtnName = "Comment" & (Replace(strLastBtnName, "Comment","") +1)

ActiveSheet.Buttons.Add(NextComment.Left, NextComment.Top, 81, 14.25).Select

Selection.Name = strNewBtnName

Selection.OnAction = "CommentButton_Click"

By the way, are you sure you need to have the buttons named sequentially?

When a new button is added it will be given a unique name, so it should be identified properly within the assigned macro.
 
Upvote 0
That's really dumb of me.

Of course you are right that if I force .onaction to the newly created button it will work with the auto-generated number. Now that you pointed this out I remembered that this fixation on Comment &i was from time that I wanted to copy buttons from template, not create new ones.

Thank you very much for your insight. Problem Solved.

All the best in 2015!
 
Upvote 0
You could name them using the row number they are located in.

Selection.Name = "Comment" & NextRow.Row
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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