I have a spreadsheet where I need to provide an ability for users to add rows through a button click. The row that is added needs to have a lot of formulas in each of the columns which are already setup in the previous row.
I've therefore written a copy paste kind of code that ensures formulas are present in the newly added rows.
I didn't bother about performance as users were supposed to add a few rows at a time in most cases. However, users are now adding 100 rows pretty frequently and I want to therefore see how best the performance can be improved.
Note: In the code below "SectionName" basically tells in which horizontal section I want to add the rows as there are multiple horizontal sections in the spreadsheet. I have some unique text setup in the last row of each of the sections which helps in getting to the last row in section and then start the copy-paste process.
Here's the code that is present currently
I've therefore written a copy paste kind of code that ensures formulas are present in the newly added rows.
I didn't bother about performance as users were supposed to add a few rows at a time in most cases. However, users are now adding 100 rows pretty frequently and I want to therefore see how best the performance can be improved.
Note: In the code below "SectionName" basically tells in which horizontal section I want to add the rows as there are multiple horizontal sections in the spreadsheet. I have some unique text setup in the last row of each of the sections which helps in getting to the last row in section and then start the copy-paste process.
Here's the code that is present currently
Code:
Sub AddRow(SectionName As String)
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim RowsToAddInput As String
Dim RowsToAdd As Integer
' Ask how many rows need to be added
RowsToAddInput = InputBox(Prompt:="How many rows do you want to add (Max 100)?", Title:="Add Rows", Default:="1")
On Error GoTo Err
' Check if an appropriate number has been entered
If RowsToAddInput = "" Then 'Nothing has been entered or User pressed cancel button
Exit Sub
ElseIf Not IsNumeric(RowsToAddInput) Then 'User did not enter a number
GoTo Err
ElseIf RowsToAddInput < 1 Or RowsToAddInput > 100 Then 'User did not enter a value between 1 and 100
GoTo Err
Else
RowsToAdd = CInt(RowsToAddInput)
Application.StatusBar = "Rows are being added... Please be patient."
Range("A1").Select
Cells.Find(What:=SectionName, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
' Start adding rows
For i = 1 To RowsToAdd
Rows(ActiveCell.Row - 1).Select 'Select the row to be copied
Selection.Copy
Rows(ActiveCell.Row).Select ' Select the row above. This is where the row will be pasted
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False 'Copy Mode
Rows(ActiveCell.Row + 1).Select 'Select the newly added row
On Error Resume Next 'to handle no constants in range
Selection.SpecialCells(xlConstants).ClearContents 'Clear only constants and not the formulas
Cells(ActiveCell.Row, 2).Select
Next i
End If
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.StatusBar = False
Exit Sub
Err:
MsgBox "Please enter a valid number between 1 and 100!"
AddRow SectionName
End Sub