Hi all,
I am in need of some fine tuning. I created this VBA code that works but it doesnt copy the formulas to the inserted row. Lastly, i currently have it as user input for the qty of rows to add.
Looking for help to just adjust this to insert 1 row and remove the dialog box also to copy the formulas into the inserted row. Thanks in advance!
I am in need of some fine tuning. I created this VBA code that works but it doesnt copy the formulas to the inserted row. Lastly, i currently have it as user input for the qty of rows to add.
Looking for help to just adjust this to insert 1 row and remove the dialog box also to copy the formulas into the inserted row. Thanks in advance!
VBA Code:
Sub AddInputRow()
subName = "AddInputRow" 'For Error handling only
On Error GoTo Nope
'Get active sheet
Dim act As Worksheet
Set act = ThisWorkbook.ActiveSheet
'prompt for suer entry and loop back if invalid
StrPrompt = "Enter number of rows to insert:"
Redo:
xNum = Application.InputBox(StrPrompt, "Insert Rows at Bottom", , , , , , Type:=1)
If xNum = 0 Or xNum = "" Or xNum = vbNullString Then
'User Cancelled and close box
ElseIf xNum < 1 Or Int(xNum) / xNum <> 1 Then
'User entered a non-positive integer
GoTo Redo
Else
'Add rows and update formattingand formulas
bot_row = act.Range("Z1")
act.Rows(bot_row & ":" & bot_row + (xNum - 1)).Insert Shift:=x1ShiftDown
act.Range("A" & bot_row - 1 & ":P" & bot_row - 1).Copy
act.Range("A" & bot_row & ":P" & bot_row + (xNum - 1)).PasteSpecial xlPasteFormats
act.Range("A" & bot_row - 1 & ":B" & bot_row - 1 & ":C" & bot_row - 1 & ":D" & bot_row - 1 & ":E" & bot_row - 1 & ":F" & bot_row - 1 & ":G" & bot_row - 1 & ":H" & bot_row - 1 & ":I" & bot_row - 1 & ":J" & bot_row - 1 & ":K" & bot_row - 1 & ":L" & bot_row - 1 & ":M" & bot_row - 1 & ":N" & bot_row - 1 & ":O" & bot_row - 1 & ":P" & bot_row - 1).Copy
act.Range("A" & bot_row - 1 & ":B" & bot_row - 1 & ":C" & bot_row - 1 & ":D" & bot_row - 1 & ":E" & bot_row - 1 & ":F" & bot_row - 1 & ":G" & bot_row - 1 & ":H" & bot_row - 1 & ":I" & bot_row - 1 & ":J" & bot_row - 1 & ":K" & bot_row - 1 & ":L" & bot_row - 1 & ":M" & bot_row - 1 & ":N" & bot_row - 1 & ":O" & bot_row - 1 & ":P" & bot_row - 1).PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
act.Range("A" & bot_row).Activate
ActiveWindow.ScrollRow = bot_row - 10
End If
Continue:
'Calculate everything once when finished and enable events
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
'Do this if error occurs
Nope:
MsgBox "An error has been logged: " & vbNewLine & ThisWorkbook.ActiveSheet.Name & vbNewLine & subName & "(Line " & Erl & ")" & vbNewLine & Err.Description
Resume Continue
End Sub