Copy and paste a range based on a cell value.

watsonla

New Member
Joined
Jul 3, 2011
Messages
23
I have a range of data in cells k2:m2
I want to paste it into cells f7:h7
But If cell d1 =1 I want to copy and insert the range once.
If D1=2 I want to insert the range twice so it would occupy F7:h7 and F8:h8 and so on.
I need this to continue to whatever the D1 cell value equals.
I need to write this as macro so a command button will recognise D1 and copy the range and paste the range "x" times always starting at F7:h7.
I would be grateful for any assistence on this please
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
try
VBA Code:
Sub sbCopy()
    myTimes = Range("D2")
    Range("F2").CurrentRegion.ClearContents 'Maybe you need this line to clear F2:H2 values.
    Range("K2:M2").Copy
    Range("F2").Resize(myTimes).PasteSpecial
    Application.CutCopyMode = False
    Range("F2").Select
End Sub
 
Upvote 0
try
VBA Code:
Sub sbCopy()
    myTimes = Range("D2")
    Range("F2").CurrentRegion.ClearContents 'Maybe you need this line to clear F2:H2 values.
    Range("K2:M2").Copy
    Range("F2").Resize(myTimes).PasteSpecial
    Application.CutCopyMode = False
    Range("F2").Select
End Sub
Thank you for the reply.
This has partially done what I needed. It has given me some clarity, but i still have an issue that I cannot resolve.
Using the code you supllied, it copied over the existing cells in the range F2:H2 and depending on the number in D2, would write over the existing data below F2:H2.

I have tried the following, but i am stuck.
My attempt below only inserts and moves the existing data down 1 line. But I need the exist data in F7:H7 and lower to move down by the same amount as cell "D2" before the data from K2:M2 is inserted multiple times as directed by the cell in D2
The bottom part of the code works perfectly, thank you.

Sub sbCopy()
myTimes = Range("D2")
Range("F7:H7").Select
Selection.Insert Shift:=xlDown


myTimes = Range("D2")
Range("F2").CurrentRegion.ClearContents 'Maybe you need this line to clear F2:H2 values.
Range("K2:M2").Copy
Range("F7").Resize(myTimes).PasteSpecial
Application.CutCopyMode = False
Range("F7").Select

End Sub
 

Attachments

  • 1670926694242.png
    1670926694242.png
    18.1 KB · Views: 2
Upvote 0
try again.
VBA Code:
Sub sbCopy()
    myTimes = Range("D2")
    Range("K2:M2").Copy
    If Range("F2") = "" Then
        Range("F2").Resize(myTimes).PasteSpecial
    Else
        Range("F" & Rows.Count).End(xlUp).Offset(1, 0).Resize(myTimes).PasteSpecial
    End If
    Application.CutCopyMode = False
    Range("F2").Select
End Sub
 
Upvote 0
try again.
VBA Code:
Sub sbCopy()
    myTimes = Range("D2")
    Range("K2:M2").Copy
    If Range("F2") = "" Then
        Range("F2").Resize(myTimes).PasteSpecial
    Else
        Range("F" & Rows.Count).End(xlUp).Offset(1, 0).Resize(myTimes).PasteSpecial
    End If
    Application.CutCopyMode = False
    Range("F2").Select
End Sub
Thanks again for your reply.
Unfortunately, this posts the data at the end of the list as show in this example.

1670946351816.png

I need the existing data to move down and the new data to be inserted at the top of the existing Data, Like below.


1670946480302.png
 
Upvote 0
How about this?
VBA Code:
Sub sbCopy()
    myTimes = Range("D2")
    Range("K2:M2").Copy
    If Range("F2") = "" Then
        Range("F2").Resize(myTimes).PasteSpecial
    Else
        Range("F2:H2").Resize(myTimes).Insert Shift:=xlDown
    End If
    Application.CutCopyMode = False
    Range("F2").Select
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

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