Copy and Paste Rows Based on Cell Value

Lindsay0385

New Member
Joined
Dec 21, 2016
Messages
30
Hi - I'm working on a spreadsheet and need help. I have Cell F30, set to be numeric data validation, specifically a number greater than or equal to 1. Here's what I'm trying to do...

If someone uses the form and enters a 1 in Cell F30, I would like nothing to be done.

If someone puts a 2 in Cell F30, I would like the spreadsheet to copy Rows 40-51 and paste once, inserting the copied rows directly below Row 51.

If someone puts a 3 in Cell F30, I would like the spreadsheet to copy Rows 40-51 and paste twice, inserting the first copied rows below Row 51, and then the next paste below that previous paste.​

And so on.​

It would be awesome, if it could automatically run as soon as a value is entered in F30, but if not, I can add a button with instructions.

Is this at all possible? Usually I start with recording a macro and then edit to my hearts desire, but I don't know how I could setup this macro via recording.

Thanks,

Lindsay
 
Last edited:
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim x   As Long
    Dim LC  As Long
    
    With Target
        If .Count > 1 Then Exit Sub
        If .Value > 1 And Not Intersect(Target, Cells(30, 6)) Is Nothing Then
        
            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With
            
            For x = 1 To .Value - 1
                LC = Cells(40, Columns.Count).End(xlToLeft).Column
                Cells(40, 1).Resize(12, LC).EntireRow.Copy Cells(Rows.Count, 1).End(xlUp).Offset(1)
                Application.CutCopyMode = False
            Next x
            
            With Application
                .ScreenUpdating = True
                .EnableEvents = True
            End With
            
        End If
    End With

End Sub
Thanks! This does the same as the other macro, but, like that one too, it replaces the data below row 51 instead of inserting the copied rows below row 51 (above row 52). Would there be a way to fix that?

Also, would there be a way to "reset" the copied areas if the number in the box is changed?
 
Last edited:
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    Dim x   As Long
    Dim LC  As Long
    
    With Target
        If .Count > 1 Then Exit Sub
        If .Value > 1 And Not Intersect(Target, Cells(30, 6)) Is Nothing Then
        
            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With
            
            For x = 1 To .Value - 1
                LC = Cells(40, Columns.Count).End(xlToLeft).Column
                With Cells(40, 1).Resize(12, LC)
                    .EntireRow.Copy
                    .Offset(.Rows.Count).EntireRow.Insert
                End With
                Application.CutCopyMode = False
            Next x
            
            With Application
                .ScreenUpdating = True
                .EnableEvents = True
            End With
            
        End If
    End With


End Sub
Also, would there be a way to "reset" the copied areas if the number in the box is changed?

Try:
Code:
[/COLOR]Sub Reset()


    Cells(52, 1).Resize(12 * Cells(30, 6).Value - 11).EntireRow.Delete


End Sub
 
Upvote 0

Forum statistics

Threads
1,216,163
Messages
6,129,223
Members
449,495
Latest member
janzablox

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