简体   繁体   English

复制动态范围的公式

[英]Copying formula for dynamic range

I have a Macro to place a division formula in a cell in row "d", and replicates the same formula all the way in row "d" till the last cell. 我有一个宏,可在“ d”行的一个单元格中放置一个除法公式,并在“ d”行中一直复制相同的公式,直到最后一个单元格。 I am trying to replicate the same formula across other columns "E:L" without having to rewrite the entire code for each column seperately 我试图在其他列“ E:L”上复制相同的公式,而不必分别重写每个列的整个代码

With wbk4.Sheets("USD")
Set clDest = .Range("D2")
Set clLookup = .Range("AA2")
End With

If clLookup.Offset(1, 0) <> vbNullString Then
rws = Range(clLookup, clLookup.End(xlDown)).Rows.Count
Set clDest = clDest.Resize(rws, 1)
End If
clDest.Formula = "=(" & clLookup.Address(False, False) & " / 3.68)"

This does the correct formula for the mentioned column "D" till the last cell. 直到最后一个单元格为止,对于上述列“ D”,这都将执行正确的公式。 What i need is this to be replicated across the following columns "E" to "L" only from the 2nd cell till the last row. 我需要的是仅从第二个单元格到最后一行在以下“ E”至“ L”列之间进行复制。

I tried to do it through the below logic, but its a bit too complicated and does not work, Im sure there is another way around :) 我试图通过下面的逻辑来做到这一点,但它有点太复杂了,无法正常工作,我确定还有另一种方法:)

With wbk4.Sheets("USD")
lrow = .Range("D" & .Rows.Count).End(xlUp).Row
.Range("D2" & lrow).Copy
wbk4.Sheets("USD").Range("E2").PasteSpecial Paste:=xlPasteFormulas
End With

For a quick and dirty solution simply replace 为了快速而又肮脏的解决方案,只需更换

Set clDest = clDest.Resize(rws, 1)

with

Set clDest = clDest.Resize(rws, x)

where x is the number of columns you want to fill. 其中x是您要填充的列数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM