复制和粘贴单元格 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=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
我需要复制单元格 A1:C10,然后粘贴 X 次。 X 在单元格 D1 中,也在单元格 A1 中,我有一个公式
=IF('C:\Users\..\..\E kompletuar\[data.xlsx]data'!$H=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