繁体   English   中英

复制和粘贴单元格 x 次并更改单元格地址

[英]Copy and Paste cells x times and change Cell address

我需要复制单元格A1:C10 ,并将它们粘贴 X 次。 X 在单元格 D1 中,也在单元格 A1 中我有一个公式

=IF('C:\Users\..\..\\E kompletuar\[data.xlsx]data'!$H$2=0;"")

我需要将单元格行 $H$ 2更改为单元格A1:C10的粘贴次数。 例如。 $H$ 3 (如果我第一次粘贴),$H$ 4 (如果我第二次粘贴),$H$ 5 (如果我第三次粘贴)...

我想这就是你所追求的

Sub foo()
Dim x as Long, numberOfTimes as Long 'number of times to "copy"
Dim rngToCopy as Range
Dim rngToPaste as Range
Dim A1_FORMULA as String

'## Define the range you want to copy:
Set rngToCopy = Range("A1:C10")

'## Get the number "X" from cell D1:
numberOfTimes = Range("D1").Value

For x = 1 to numberOfTimes

    '## Define your base formula:
    A1_FORMULA = "=IF('C:\Users\..\..\\E kompletuar\[data.xlsx]data'!$H$" & CStr(2 + x) & "=0;"""")"

    '# Determine where to paste:
    Set rngToPaste  = rngToCopy.Offset(x*(rngToCopy.Rows.Count + 2))

    '# Copy and Paste
    rngToCopy.Copy Destination:=rngToPaste

    '# Update the formula in the first copied cell/column A:
    rngToPaste.Cells(1,1).Formula = A1_FORMULA

Next
End Sub

暂无
暂无

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

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