Active X Button: Insert new row below with formulas and formatting

sam987654

New Member
Joined
Feb 8, 2020
Messages
11
Office Version
  1. 365
  2. 2019
Platform
  1. MacOS
Hello all,

I've done the work of inserting my own Active X command button (titled "New Entry"), and I was able to get it to work by applying the following code:

Private Sub CommandButton1_Click()
Rows(11).Insert
End Sub


This inserts a new row at Row 15, but it is a blank, default row. I'd like it to include all formulas and formatting that hidden Row 14 has. Here is a screenshot of my document

1618414447824.png


I've tried looking this up many times, but can't figure it out. Would anyone be willing to share answer?
 

Attachments

  • 1618414205195.png
    1618414205195.png
    48.6 KB · Views: 46

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello Sam, welcome to the MrExcel forum.
Try with this code...
VBA Code:
Private Sub CommandButton1_Click()
  
    Dim vC As Integer
  
    With ActiveSheet
        .Rows(15).Insert
        .Rows(14).EntireRow.Copy Rows(15)
        For vC = 1 To .Cells(14, Columns.Count).End(xlToLeft)
            If Not .Cells(15, vC).HasFormula Then _
                .Cells(15, vC).ClearContents
        Next
    End With

End Sub
 
Upvote 0
Hello Sam, welcome to the MrExcel forum.
Try with this code...
VBA Code:
Private Sub CommandButton1_Click()
 
    Dim vC As Integer
 
    With ActiveSheet
        .Rows(15).Insert
        .Rows(14).EntireRow.Copy Rows(15)
        For vC = 1 To .Cells(14, Columns.Count).End(xlToLeft)
            If Not .Cells(15, vC).HasFormula Then _
                .Cells(15, vC).ClearContents
        Next
    End With

End Sub
Hi Max,

Thank you for responding. I see the new row inserting itself, but I see it behind this error message. Thoughts?

1618503442558.png
 
Upvote 0
Try to copy and paste my code, do not type.
My first thought is error in typing (X1TOLEFT) instead (XLTOLEFT).
Also try to create new button and paste this code.
 
Upvote 0
You're missing the .Column on this line
VBA Code:
        For vC = 1 To .Cells(14, Columns.Count).End(xlToLeft).Column
 
Upvote 0
Yes, I forgot that.
I have some numbers in the column "K" and it couse code in my sheet works properly.
Thanks for the correction.
 
Upvote 0
You could also get rid of the loop like
VBA Code:
    With ActiveSheet
        .Rows(15).Insert
        .Rows(14).EntireRow.Copy .Rows(15)
       .Rows(15).SpecialCells(xlConstants).ClearContents
    End With
 
Upvote 0
You're missing the .Column on this line
VBA Code:
        For vC = 1 To .Cells(14, Columns.Count).End(xlToLeft).Column
You're missing the .Column on this line
VBA Code:
        For vC = 1 To .Cells(14, Columns.Count).End(xlToLeft).Column
Welp! It looks like this worked. Thank you both so much. The only remaining thing.... I want to copy Row 14 but also keep it hidden. What's happening is that any new rows created are also staying hidden. I'm having to unhide Row 14 for the others to show

You could also get rid of the loop like
VBA Code:
    With ActiveSheet
        .Rows(15).Insert
        .Rows(14).EntireRow.Copy .Rows(15)
       .Rows(15).SpecialCells(xlConstants).ClearContents
    End With
You could also get rid of the loop like
VBA Code:
    With ActiveSheet
        .Rows(15).Insert
        .Rows(14).EntireRow.Copy .Rows(15)
       .Rows(15).SpecialCells(xlConstants).ClearContents
    End With
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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