Inserting multiple rows based off value in a cell

MrBalls1983

New Member
Joined
Jan 23, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello
I'm trying to create a Form button that will insert an exact copy of a single row (formales, values and formating). The number of rows to insert is to be determined by a value in a cell. I've screenshot my worksheet below.

Basically cell L4 contains how many rows i want to add. Row A9:W9 is the row i would like to base the row insert off.

any help would be much appreciated.

1737616048896.png
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello @MrBalls1983.
Maybe
VBA Code:
Option Explicit

Sub AddRowsBasedOnTemplate()
    Dim i           As Long

    Dim ws          As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")    ' Replace Sheet1 with the name of your worksheet

    Dim templateRow As Range
    Set templateRow = ws.Range("A9:W9")

    Dim insertCount As Long
    insertCount = ws.Range("L4").Value

    If Not IsNumeric(insertCount) Or insertCount <= 0 Then
        MsgBox "Please specify the correct number of rows in the cell L4.", vbExclamation
        Exit Sub
    End If

    For i = 1 To insertCount
        templateRow.Offset(1).EntireRow.Insert
        templateRow.Copy templateRow.Offset(1)
    Next i

    ' Clear the contents of the pasted rows if needed, but leave the formatting
    templateRow.Offset(1).Resize(insertCount, templateRow.Columns.Count).ClearContents
    MsgBox "Added " & insertCount & " row(s).", vbInformation

    ' Freeing up memory
    Set templateRow = Nothing
    Set ws = Nothing
End Sub
But I'm not sure I understood you correctly. I hope so and I helped you. Good luck.
 
Upvote 0
Hello @MrBalls1983.
Maybe
VBA Code:
Option Explicit

Sub AddRowsBasedOnTemplate()
    Dim i           As Long

    Dim ws          As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")    ' Replace Sheet1 with the name of your worksheet

    Dim templateRow As Range
    Set templateRow = ws.Range("A9:W9")

    Dim insertCount As Long
    insertCount = ws.Range("L4").Value

    If Not IsNumeric(insertCount) Or insertCount <= 0 Then
        MsgBox "Please specify the correct number of rows in the cell L4.", vbExclamation
        Exit Sub
    End If

    For i = 1 To insertCount
        templateRow.Offset(1).EntireRow.Insert
        templateRow.Copy templateRow.Offset(1)
    Next i

    ' Clear the contents of the pasted rows if needed, but leave the formatting
    templateRow.Offset(1).Resize(insertCount, templateRow.Columns.Count).ClearContents
    MsgBox "Added " & insertCount & " row(s).", vbInformation

    ' Freeing up memory
    Set templateRow = Nothing
    Set ws = Nothing
End Sub
But I'm not sure I understood you correctly. I hope so and I helped you. Good luck.
Thankyou for your help. Its coming up with an error when I run it.

1737690270775.png
 
Upvote 0
Hi. There are no errors on my side. Perhaps in your original file the 9th row is not (A9:W9).
Inserting multiple rows based off value in a cell_v1.png
Inserting multiple rows based off value in a cell_v2.png
Inserting multiple rows based off value in a cell_v3.png
Inserting multiple rows based off value in a cell_v4.png
 
Upvote 0

Forum statistics

Threads
1,226,049
Messages
6,188,566
Members
453,484
Latest member
jlo1673

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