[英]Copying formula to a range of cells and converting it to a value
I am trying to copy the formula from D1 and paste it till last row(26446) value may increase 我试图从D1复制公式并粘贴它直到最后一行(26446)值可能会增加
Dim lrow As Double
lrow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Range("D2").Copy Destination:=Range("D2:D" & lrow)
Range("D2" & "lrow").Copy 'THIS LINE SHOWS 1004 error
Range("D2:D" & "lrow").PasteSpecial Paste:=xlPasteValues
How to solve this 怎么解决这个问题
Anything within quotes will be considered as a string so "lrow" is considered as a String
and not a variable. 引号内的任何内容都将被视为字符串,因此“lrow”被视为
String
而不是变量。
It should be 它应该是
Range("D" & lrow).Copy
or whatever range you are trying to copy. 或者你试图复制的任何范围。 I think, you are trying
我想,你在努力
Range("D2:D" & lrow).Copy
The other simple alternative to 另一个简单的替代方案
Range("D2:D" & "lrow").Copy
Range("D2:D" & "lrow").PasteSpecial Paste:=xlPasteValues
is 是
Range("D2:D" & lrow).Value = Range("D2:D" & lrow).Value
You have already got two excellent answers and hence this is not an answer but an enhancement to your code. 您已经有两个很好的答案,因此这不是答案,而是对代码的增强。
You don't need to declare the row variable as Double
. 您不需要将行变量声明为
Double
。 Long
is good enough. 好
Long
就够了。 You may want to read up on Data type HERE 您可能希望在此处阅读数据类型
Your code can be reduced to this 您的代码可以简化为此
Code 码
BTW, Your question says D1
but your code is taking D2
into consideration. 顺便说一下,你的问题是
D1
但你的代码正在考虑D2
。 So I am going with D2
. 所以我要去
D2
。 If it is D1
then replace D2
by D1
below. 如果是
D1
则将D2
替换为D1
。
Sub Sample()
Dim lrow As Long
With Sheets("Sheet2")
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("D2:D" & lrow).Formula = .Range("D2").Formula
.Range("E2:E" & lrow).Formula = .Range("E2").Formula
.Range("F2:F" & lrow).Formula = .Range("F2").Formula
.Range("G2:G" & lrow).Formula = .Range("G2").Formula
.Range("D2:G" & lrow).Value = .Range("D2:G" & lrow).Value
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.