insert row code tweak required

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,066
Office Version
  1. 365
Platform
  1. Windows
Code:
Sub InsertRow()
    On Error GoTo InsertRow_Error
    With ActiveSheet
        .Unprotect Password:="password"
        Dim rown As Range
        Dim rname As String
        Set rown = Application.InputBox _
        (Prompt:="Select a cell in column A where you want the new row to be added", Title:="Add a new row", Type:=8)
        rown.Select
        Selection.EntireRow.Insert
        ActiveCell.Value = ActiveCell.Offset(-1, 0).Value
        ActiveCell.Offset(0, 41).FormulaR1C1 = "=COUNTA(RC[-31]:RC[-1])"
        ActiveCell.Offset(0, 42).FormulaR1C1 = "=RC[-38]/7*RC[-1]"
        ActiveCell.Offset(0, 43).FormulaR1C1 = "=RC[+2]/7*RC[-2]"

InsertRow_Error:
        .Protect Password:="password", AllowFormattingCells:=True
    End With
End Sub

I've been using this code for quite a while now, but users being users, they sometimes click on column B, C etc rather than A.

The code replaces the usual insert row function, adding the necessary formulae.

Can the above be tweaked so that it always starts from Column A even if they click elsewhere?

TIA
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Perhaps something like this.
Code:
Sub InsertRow()
Dim rown As Range
Dim rname As String

    On Error GoTo InsertRow_Error
    With ActiveSheet
        .Unprotect Password:="password"
        Set rown = Application.InputBox _
        (Prompt:="Select a cell in column A where you want the new row to be added", Title:="Add a new row", Type:=8)
        
        Set rown = Cells(rown.Row, "A")
        
        With rown.EntireRow.Insert
            .Value = ActiveCell.Offset(-1, 0).Value
            .Offset(0, 41).FormulaR1C1 = "=COUNTA(RC[-31]:RC[-1])"
            .Offset(0, 42).FormulaR1C1 = "=RC[-38]/7*RC[-1]"
            .Offset(0, 43).FormulaR1C1 = "=RC[+2]/7*RC[-2]"
        End With
InsertRow_Error:
        .Protect Password:="password", AllowFormattingCells:=True
    End With
    
End Sub
 
Upvote 0
Hi

That code inserted the row but inserted no formulae from this;

.Value = ActiveCell.Offset(-1, 0).Value
.Offset(0, 41).FormulaR1C1 = "=COUNTA(RC[-31]:RC[-1])"
.Offset(0, 42).FormulaR1C1 = "=RC[-38]/7*RC[-1]"
.Offset(0, 43).FormulaR1C1 = "=RC[+2]/7*RC[-2]"
 
Upvote 0
Oops, my bad.:eek:

Try this.
Code:
Sub InsertRow()
Dim rown As Range
Dim rname As String

    On Error GoTo InsertRow_Error
    With ActiveSheet
        .Unprotect Password:="password"
        Set rown = Application.InputBox _
        (Prompt:="Select a cell in column A where you want the new row to be added", Title:="Add a new row", Type:=8)
        
        Set rown = Cells(rown.Row, "A")
        
        With rown
            .EntireRow.Insert
            .Value = ActiveCell.Offset(-1, 0).Value
            .Offset(0, 41).FormulaR1C1 = "=COUNTA(RC[-31]:RC[-1])"
            .Offset(0, 42).FormulaR1C1 = "=RC[-38]/7*RC[-1]"
            .Offset(0, 43).FormulaR1C1 = "=RC[+2]/7*RC[-2]"
        End With
InsertRow_Error:
        .Protect Password:="password", AllowFormattingCells:=True
    End With
    
End Sub
 
Last edited:
Upvote 0
Many thanks, I've got it working now :-)
 
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,057
Members
453,524
Latest member
AshJames

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