Duplicate Command Button

Bkisley

Board Regular
Joined
Jan 5, 2017
Messages
100
Is there a way I can create a command button and then copy and paste that button several more times to other areas on the same tab without having to redo the code each time?

I created a command button that when clicked does the following
Insert row below this row
removes format from the row above
carries down formula from the row above

I need to do this process 29 more times. When I copy and paste this button down to the next section, (2 of the total 30 times) it does the correct process, however it goes back to the row where the first button began. I need this process to start over at the row each button is.

The other issue I am running into is when I click on the command button to do the process shown above, when I click on that same button again it inserts a row directly below the first row thus pushing down my new data down a row. I want a row inserted below the previously inserted row from the first click

Please Help!
Thanks
 
Last edited:
YES!! This is it! Awesome, thank you very much!

As for the undo issue, just thinking out loud now, could the first step of the macro be a "save" and then do the insert row and drop formulas down? That way if it was clicked on accident you could go back to the state it was at before the button was clicked?
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You're welcome!

You certainly can:
Code:
Sub onedown()
    Dim rw As Integer
    Dim lastCol As Integer
    
    ActiveWorkbook.Save
    
    rw = Selection.Row + 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Range("A1").SpecialCells (xlCellTypeBlanks)
    Rows(rw).Insert CopyOrigin:=xlFormatFromLeftOrAbove
    
    With Cells(rw, 1).Resize(, lastCol)
        .Interior.ThemeColor = xlThemeColorDark1
        .Borders.LineStyle = xlNone
    End With
    
    Range("L" & rw).FormulaR1C1 = Range("L" & rw - 1).FormulaR1C1
    Range("O" & rw).FormulaR1C1 = Range("O" & rw - 1).FormulaR1C1
    Range("I" & rw).Select
End Sub
I think it would be even easier to select the row that was created and delete the row manually, but that's just a matter of preference.
 
Last edited:
Upvote 0
Would the code you wrote be easy enough to make a change to it? Instead of inserting one row below what cell I am in it would unhide only one row below the cell I am in. This would unhide one row at a time even if there are 15 rows hidden.
 
Upvote 0
Yeah, that isn't too hard. However, if the next row isn't hidden, it won't go looking further down for the first hidden row (unless we adjust the code to loop to the first hidden row).
Code:
Sub onedown()
    Dim rw As Integer
    Dim lastCol As Integer
    
    ActiveWorkbook.Save
    
    rw = Selection.Row + 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    'Rows(rw).Insert CopyOrigin:=xlFormatFromLeftOrAbove
    If Range("A" & rw).Hidden = True Then
        Range("A" & rw).EntireRow.Hidden = False
    End If
    
    With Cells(rw, 1).Resize(, lastCol)
        .Interior.ThemeColor = xlThemeColorDark1
        .Borders.LineStyle = xlNone
    End With
    
    Range("L" & rw).FormulaR1C1 = Range("L" & rw - 1).FormulaR1C1
    Range("O" & rw).FormulaR1C1 = Range("O" & rw - 1).FormulaR1C1
    Range("I" & rw).Select
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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