简体   繁体   English

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

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

I need to copy cells A1:C10 , and paste them X times.我需要复制单元格A1:C10 ,并将它们粘贴 X 次。 The X is in the cell D1, also in the cell A1 i have a formula X 在单元格 D1 中,也在单元格 A1 中我有一个公式

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

I need the Cell row $H$ 2 to change by the number of times the cells A1:C10 is pasted.我需要将单元格行 $H$ 2更改为单元格A1:C10的粘贴次数。 eg.例如。 $H$ 3 (if i paste for first time), $H$ 4 (if i paste for second time),$H$ 5 (if i paste for third time) ... $H$ 3 (如果我第一次粘贴),$H$ 4 (如果我第二次粘贴),$H$ 5 (如果我第三次粘贴)...

I think this is what you're after我想这就是你所追求的

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