Adding a specific row with any cell location in a VBA

mholton63

New Member
Joined
Nov 16, 2018
Messages
9
I'd like to insert a row after row 3 no matter where my highlighted cell is. The VBA below will only work if I have cell 4 picked. Is there a way to accomplish what I'm wanting?


Sub Insert_Rows()
With Selection
If .Areas.Count = 1 Then
.EntireRow.Insert
Intersect(.EntireRow.Offset(-.Rows.Count - 1).Resize(.Rows.Count + 1), Range("C:U")).FillDown
End If
End With
End Sub

Kind Regards,
Mark
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
replace selection with 1 or several cells of row 4, counting areas is no longer needed
VBA Code:
Sub Insert_Rows()
     With Range("A4")
          .EntireRow.Insert
          Intersect(.EntireRow.Offset(-.Rows.Count - 1).Resize(.Rows.Count + 1), Range("C:U")).FillDown
     End With
End Sub
 
Upvote 0
Thanks, That worked great. Is there any way to protect the sheet and keep the VBA working? I keep getting an error 400.
 
Upvote 0
you have to say one time during a session that the file is open this
VBA Code:
Sheets("MySheet").Protect userinterfaceonly:=True
So the best place to do is perhaps in the "Workbook_open" event in ThisWorkbook, but it can be everywhere else also ....
 
Upvote 0
Try to protect this way:

VBA Code:
Activesheet.protect Userinterfaceonly:=true
 
Upvote 0
Since your range is fixed, you can also do

VBA Code:
Sheet1.Rows(4).Insert

Change Sheet1 to the relevant sheet codename. Also since your row number is fixed, instead of

VBA Code:
Intersect(.EntireRow.Offset(-.Rows.Count - 1).Resize(.Rows.Count + 1), Range("C:U")).FillDown

you can also do

VBA Code:
Intersect(Range("3:4"), Range("C:U")).FillDown

As mentioned by other users, you may use `Userinterfaceonly:=true` or simply protect and unprotect in the code as well. Here is an example

VBA Code:
Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim MyPass As String
   
    MyPass = "SomePassword"
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
   
    With ws
        .Unprotect MyPass
        If Range("A4").Areas.Count = 1 Then
            .Rows(4).Insert
            Intersect(.Range("3:4"), .Range("C:U")).FillDown
        End If
        .Protect MyPass
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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