Add dynamic button to copy rows VBA MACRO

AhmedGRE

New Member
Joined
Jan 31, 2018
Messages
2
Hi,

I am attempting to create a table where costs can be calculated by the user. These are spread into different categories.

I have created a macro using relative reference and assigned this to a button which allows a new line to be entered into the table every time the button is pressed but this means a specific cell needs to be highlighted. My intention was to have the highlighted or referenced cell to be the one which the button is on. This way i can copy the button elsewhere and based on the relative reference it should create copies of the row above when clicked.

My knowledge of macros and VBA is very limited but the code is:
File-Copy-icon.png

<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub Macro2()
'
' Macro2 Macro
'

'
ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
Selection.Copy
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
ActiveCell.Offset(2, 6).Range("A1").Select
End Sub</code>

I guess ActiveCell should somehow refer to the Button cell instead.

UPDATE: i have attempted to use the code from some online forums below to help with my understanding. However, i seem to get an error:
Run-time error '1004':
Unable to get the Buttons property pf the worksheet class

Code:
[COLOR=#141414][FONT=&quot]Public Sub Button_Click()[/FONT][/COLOR]
[COLOR=#141414][FONT=&quot]With ActiveSheet.Buttons(Application.Caller)[/FONT][/COLOR]
[COLOR=#141414][FONT=&quot]Cells(.TopLeftCell.Row, .BottomRightCell.Column + 1).Value = "X"[/FONT][/COLOR]
[COLOR=#141414][FONT=&quot]End With[/FONT][/COLOR]
[COLOR=#141414][FONT=&quot]End Sub[/FONT][/COLOR]

Any ideas?

Thanks.

556345d1516116091-add-dynamic-button-to-copy-rows-vba-macro-nonstaff-costs-table.jpg
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Apologies.

Code:
[COLOR=#333333][FONT=monospace]Sub Macro2()[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]'[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]' Macro2 Macro[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]'[/FONT][/COLOR]

[COLOR=#333333][FONT=monospace]'[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]Selection.Copy[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]Selection.Insert Shift:=xlDown[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]Application.CutCopyMode = False[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]ActiveCell.Offset(2, 6).Range("A1").Select[/FONT][/COLOR]
[COLOR=#333333][FONT=monospace]End Sub[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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