复制一个列中的公式,并粘贴到每第n列。

huangapple go评论90阅读模式
英文:

Copying formulas in one column and pasting every nth column

问题

我试图复制包含成千上万行每行都有vlook-up的B列,并粘贴到每隔3列的后续列中。

我找到了这段代码,它可以工作,但它会将其粘贴为数值,我需要将vlook-up公式粘贴进去。

  1. Sub Sco__copy()
  2. Dim cpval As Range
  3. Dim LastRow As Long
  4. With Worksheets("Sheet8")
  5. LastRow = .Cells(Rows.Count, "B:B").End(xlUp).Row
  6. Set cpval = .Range("B12:B" & LastRow)
  7. For colx = 2 To 40 Step 3
  8. .Range(.Cells(12, colx), .Cells(LastRow, colx)).Formula = cpval.Formula
  9. Next
  10. End With
  11. End Sub
英文:

I am trying to copy column B which contains thousands of rows with vlook-ups in each row and paste it into every following 3rd column.

I found this code which works but it's pasting as values, I need the vlook-up formulas to be pasted.

  1. Sub Sco__copy()
  2. Dim cpval As Range
  3. Dim LastRow As Long
  4. With Worksheets("Sheet8")
  5. LastRow = .Cells(Rows.Count, "B:B").End(xlUp).Row
  6. Set cpval = .Range("B12:B" & LastRow)
  7. For colx = 2 To 40 Step 3
  8. .Range(.Cells(12, colx), .Cells(LastRow, colx)).Value = cpval.Value
  9. Next
  10. End With
  11. End Sub

答案1

得分: 1

这应该适用于您,只需将“value”更改为“formula”。

  1. Sub Sco__copy()
  2. Dim cpval As Range
  3. Dim LastRow As Long
  4. With Worksheets("Sheet8")
  5. LastRow = .Cells(Rows.Count, "B:B").End(xlUp).Row
  6. Set cpval = .Range("B12:B" & LastRow)
  7. For colx = 2 To 40 Step 3
  8. .Range(.Cells(12, colx), .Cells(LastRow, colx)).Formula = cpval.Formula
  9. Next
  10. End With
  11. End Sub
英文:

This should work for you, just change "value" to "formula".

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. Sub Sco__copy()
  2. Dim cpval As Range
  3. Dim LastRow As Long
  4. With Worksheets(&quot;Sheet8&quot;)
  5. LastRow = .Cells(Rows.Count, &quot;B:B&quot;).End(xlUp).Row
  6. Set cpval = .Range(&quot;B12:B&quot; &amp; LastRow)
  7. For colx = 2 To 40 Step 3
  8. .Range(.Cells(12, colx), .Cells(LastRow, colx)).Formula = cpval.Formula
  9. Next
  10. End With
  11. End Sub

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 06:13:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定