I am running a copy paste macro but when I run it, it keeps changing the range of my formula to the last of row.
For example, my original VLOOKUP formula looks from $D$2:$G$5000
, but when I run my macro it will change it to $D$2:$G$1254
where 1254 is the last row where data resides.
Here is the copy + paste function:
Sub START1()
Dim shCurrentWeek As Worksheet
Dim shPriorWeek As Worksheet
Dim lr As Long
Set shCurrentWeek = ActiveWorkbook.Sheets("Current Week")
Set shPriorWeek = ActiveWorkbook.Sheets("Prior Week")
lr = shCurrentWeek.Range("A" & Rows.Count).End(xlUp).Row
'Copies Current Week into Prior Week and deletes Rows in Prior week
shCurrentWeek.Range("A4:X" & lr).Copy
shPriorWeek.Range("A2").PasteSpecial xlPasteValues
shPriorWeek.Range("A" & lr - 2 & ":A10000").EntireRow.Delete
End Sub
any ideas?
You can use the INDIRECT() function so the formula will always reference the rows you wish. Here's a simple example (please see this link for way more detailed info):
=SUM(INDIRECT("A1:A6"))
For a start you are using pastevalues so you will not be getting any formulas on your second page but your error lies with the last row of your code
shPriorWeek.Range("A" & lr - 2 & ":A10000").EntireRow.Delete
Because your formula overlaps this range that you are deleting excel automatically adjusts the vlookup formula.
I would suggest deleting your data before pasting the values.
*edit after reading the comments if the formulas are already on the page then this solution will not work and I would use clearcontents instead as per tim's suggestion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.