Change fixed range to dynamic in copy paste VBA Code

An Quala

Board Regular
Joined
Mar 21, 2022
Messages
146
Office Version
  1. 2021
Platform
  1. Windows
Hello, This is a basic template copy paste code, but I want to change the fixed range of this code to the dynamic range, where Cells(2,"A") is fixed always, but Cells(5,"Z") will always change depending on the template provided, so it should take the last column and the last row instead of fixed 5 and Z, also row #1 is fixed and additionally I want an empty row after each copy of the template, thank you.

VBA Code:
Sub CopyData()
Dim lRow As Long
Dim Num As Integer
Num = InputBox("How many Times")
lRow = 1

Do While Num > 0

Range(Cells(2, "A"), Cells(5, "Z")).Copy
Range(Cells(lRow + 1, "A"), Cells(lRow + 1, "Z")).Select
Selection.Insert Shift:=xlDown

lRow = lRow + 4
Num = Num - 1

Loop

Application.CutCopyMode = False
Range("A1").Select

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Is this correct? Hope this helps.
I added "On Error Resume Next" because it stops with an error when the user presses the cancel button in the InputBox.
VBA Code:
Sub CopyData()
Dim LR As Long, LC As Long, i As Long
Dim Num As Integer
On Error Resume Next
Num = InputBox("How many Times")
On Error GoTo 0
LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(Rows.Count, 1).End(xlToLeft).Column

For i = 1 To Num
    Range(Range("A2"), Cells(LR, LC)).Copy Cells(Cells(Rows.Count, 1).End(xlUp).Row + 2, 1)
Next

End Sub
 
Last edited:
Upvote 0
Is this correct?
Hi, it is duplicating the first column only, please check

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1ProductEntityOperationCampaign IdAd Group IdPortfolio IdAd IdKeyword IdProduct Targeting IdCampaign NameAd Group NameStart DateEnd DateTargeting TypeStateDaily BudgetSKUASINAd Group Default BidBidKeyword TextMatch TypeBidding StrategyPlacementPercentageProduct Targeting Expression
2Sponsored ProductsCampaignCreateSP - KeywordSP - Keyword20220207MANUALenabled100Dynamic bids - down only
3Sponsored ProductsBidding AdjustmentCreateSP - KeywordDynamic bids - down onlyplacementProductPage0
4Sponsored ProductsBidding AdjustmentCreateSP - KeywordDynamic bids - down onlyplacementTop0
5Sponsored ProductsAd GroupCreateSP - KeywordSP - Keyword - GroupSP - Keyword - Groupenabled0.5
6Sponsored ProductsProduct AdCreateSP - KeywordSP - Keyword - GroupenabledSKU 1
7Sponsored ProductsKeywordCreateSP - KeywordSP - Keyword - Groupenabled0.5keyword 1Exact
8
9Sponsored Products
10Sponsored Products
11Sponsored Products
12Sponsored Products
13Sponsored Products
14Sponsored Products
15
16Sponsored Products
17Sponsored Products
18Sponsored Products
19Sponsored Products
20Sponsored Products
21Sponsored Products
22
23Sponsored Products
24Sponsored Products
25Sponsored Products
26Sponsored Products
27Sponsored Products
28Sponsored Products
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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