Force a user to select the entire row from row number

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Can somebody help a bit please, I have cobbled together the below code to allow the user to insert extra rows. But I have a couple of problems.

1. I need be able to force the user to select the entire row, because at the moment if they select a cell not the row, the code just inserts a copy the active cell. I did try “ActiveCell.EntireRow.Copy” but this errored out on the next line “Selection.Insert Shift:=xlDown”.

2. The other part I would like to achieve is I would like to put something in column O like (do not insert rows here) so the code would check for this value and prevent any rows being entered, with a msgbox stating that you cannot insert rows here. This will enable me to protect parts of the sheet from the user inserting rows.

Any help is much appreciated

Code:
Sub AddRows()

    Dim n As Integer
    Dim Ans As Variant
'    On Error Resume Next

ActiveSheet.Unprotect
 Ans = MsgBox("Have you selected the entire row where you want to add the extra rows", vbYesNo)
 If Ans = vbYes Then

    n = InputBox("How many rows do you require?")
        If n >= 1 Then
            For numtimes = 1 To n
              Selection.Copy
    Selection.Insert Shift:=xlDown

Next
End If
ActiveSheet.Protect

Else
ActiveSheet.Protect
   Exit Sub
   End If
End Sub
 
Footoo
Tried your suggestion works brilliantly, much neater
Thank you

Code:
Sub AddRows()
Dim n As Variant, Ans As Variant
 
If TypeName(Selection) <> "Range" Then
    MsgBox "Select a worksheet cell."
    Exit Sub
ElseIf Selection.Rows.Count > 1 Then
    MsgBox "Select in one row only."
    Exit Sub
ElseIf Selection.Row < 14 Then
    MsgBox "The selection must not be in the header rows"
    Exit Sub
ElseIf Not Intersect(Selection, Range("Last_Rows")) Is Nothing Then ' this is the named range/area (Last_Rows) on the sheet
    MsgBox "The selection must not be in the formula rows at the bottom of the sheet,"
    Exit Sub
End If
 
Ans = MsgBox("Is your selection in the row where you want to add the extra rows", vbYesNo)
If Ans = vbNo Then Exit Sub
Application.DisplayAlerts = False
n = Application.InputBox(Prompt:="How many rows do you require?", Type:=1)
Application.DisplayAlerts = True
If TypeName(n) = "Boolean" Or n = 0 Then Exit Sub
n = Val(n)
If n > 49 Then
    MsgBox "Enter a number less than 50"
    Exit Sub
End If
ActiveSheet.Unprotect
Selection.EntireRow.Copy
Selection.Resize(n).EntireRow.Insert
ActiveSheet.Protect
End Sub
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Could somebody help please?
I would like to change this part of the below code
Code:
Selection.EntireRow.Copy
Selection.Resize(n).EntireRow.Insert
So it just copies and inserts the row with formats only (does not copy any text) so in effect when the user insert rows they will be blank but formatted.
I have tried
Code:
Selection.EntireRow.Copy = xlFormatsOnly
but that does not work at all
I have seen some examples but they are using the paste method
Any help is very much appreciated



Code:
Sub AddRows()
Dim n As Variant, Ans As Variant
 
If TypeName(Selection) <> "Range" Then
    MsgBox "Select a worksheet cell."
    Exit Sub
ElseIf Selection.Rows.Count > 1 Then
    MsgBox "Select in one row only."
    Exit Sub
ElseIf Selection.Row < 14 Then
    MsgBox "The selection must not be in the header rows"
    Exit Sub
ElseIf Not Intersect(Selection, Range("Last_Rows")) Is Nothing Then ' this is the named range/area (Last_Rows) on the sheet
    MsgBox "The selection must not be in the formula rows at the bottom of the sheet,"
    Exit Sub
End If
 
Ans = MsgBox("Is your selection in the row where you want to add the extra rows", vbYesNo)
If Ans = vbNo Then Exit Sub
Application.DisplayAlerts = False
n = Application.InputBox(Prompt:="How many rows do you require?", Type:=1)
Application.DisplayAlerts = True
If TypeName(n) = "Boolean" Or n = 0 Then Exit Sub
n = Val(n)
If n > 49 Then
    MsgBox "Enter a number less than 50"
    Exit Sub
End If
ActiveSheet.Unprotect
Selection.EntireRow.Copy
Selection.Resize(n).EntireRow.Insert
ActiveSheet.Protect
End Sub
 
Upvote 0
Code:
Selection.EntireRow.Copy
With Selection.Resize(n).EntireRow
    .Insert
    .Offset(-n).ClearContents
End With
 
Upvote 0
Not working exactly it seems to be inserting an extra row (which isn’t formatted the cells are unmerged on the extra row)
The other row inserted is OK

Code:
Sub AddRows()
Dim n As Variant, Ans As Variant
 
If TypeName(Selection) <> "Range" Then
    MsgBox "Select a worksheet cell."
    Exit Sub
ElseIf Selection.Rows.Count > 1 Then
    MsgBox "Select in one row only."
    Exit Sub
ElseIf Selection.Row < 14 Then
    MsgBox "The selection must not be in the header rows"
    Exit Sub
ElseIf Not Intersect(Selection, Range("Last_Rows")) Is Nothing Then ' this is the named range/area (Last_Rows) on the sheet
    MsgBox "The selection must not be in the formula rows at the bottom of the sheet,"
    Exit Sub
End If

Ans = MsgBox("Is your selection in the row where you want to add the extra rows", vbYesNo)
If Ans = vbNo Then Exit Sub
Application.DisplayAlerts = False
n = Application.InputBox(Prompt:="How many rows do you require?", Type:=1)
Application.DisplayAlerts = True
If TypeName(n) = "Boolean" Or n = 0 Then Exit Sub
n = Val(n)
If n > 49 Then
MsgBox "Enter a number less than 50"
Exit Sub
End If
ActiveSheet.Unprotect
Selection.EntireRow.Copy
With Selection.Resize(n).EntireRow
.Insert
.Offset(-n).ClearContents
End With
Selection.Resize(n).EntireRow.Insert
ActiveSheet.Protect
End Sub
 
Upvote 0
You should have replaced this line : Selection.Resize(n).EntireRow.Insert

Delete it.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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