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

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

Copying formulas in one column and pasting every nth column

问题

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

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

Sub Sco__copy()

    Dim cpval As Range
    Dim LastRow As Long

    With Worksheets("Sheet8")
        LastRow = .Cells(Rows.Count, "B:B").End(xlUp).Row
        Set cpval = .Range("B12:B" & LastRow)

        For colx = 2 To 40 Step 3
            .Range(.Cells(12, colx), .Cells(LastRow, colx)).Formula = cpval.Formula
        Next
    End With
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.

Sub Sco__copy()

    Dim cpval As Range
    Dim LastRow As Long

    With Worksheets("Sheet8")
        LastRow = .Cells(Rows.Count, "B:B").End(xlUp).Row
        Set cpval = .Range("B12:B" & LastRow)

        For colx = 2 To 40 Step 3
            .Range(.Cells(12, colx), .Cells(LastRow, colx)).Value = cpval.Value
        Next
End With
End Sub

答案1

得分: 1

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

Sub Sco__copy()

    Dim cpval As Range
    Dim LastRow As Long

    With Worksheets("Sheet8")
        LastRow = .Cells(Rows.Count, "B:B").End(xlUp).Row
        Set cpval = .Range("B12:B" & LastRow)

        For colx = 2 To 40 Step 3
            .Range(.Cells(12, colx), .Cells(LastRow, colx)).Formula = cpval.Formula
        Next
    End With

End Sub
英文:

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

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

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

Sub Sco__copy()

    Dim cpval As Range
    Dim LastRow As Long

    With Worksheets(&quot;Sheet8&quot;)
        LastRow = .Cells(Rows.Count, &quot;B:B&quot;).End(xlUp).Row
        Set cpval = .Range(&quot;B12:B&quot; &amp; LastRow)

        For colx = 2 To 40 Step 3
            .Range(.Cells(12, colx), .Cells(LastRow, colx)).Formula = cpval.Formula
        Next
    End With

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:

确定