简体   繁体   English

VBA将公式复制到最后一行然后下一个列

[英]VBA Copy Formula to Last Row Then Next Column

I'm working with the code below, and where I'm having a hard time getting this around is the piece in brackets [CURRENT COLUMN] 我正在使用下面的代码,而在此方面遇到困难的地方是方括号[CURRENT COLUMN]

I can't quite seem to get it to use the currently used column to paste the formula down. 我似乎不太明白使用当前使用的列将公式粘贴下来。

The goal is to go from columns K to END then if there is a formula in row 2 of that column, copy that formula down and proceed to the next column. 目标是从K列到END然后如果该列的第2行中有公式,则将该公式向下复制并继续到下一列。

Option Explicit
Sub recalcdash()
Dim oWkbk As Workbook
Dim oWkst As Worksheet
Dim oRng As Range
Dim LastCol As Long
Dim LastRow As Long
Dim StartCol As Integer
Dim StartRow As Long


StartCol = 11
Set oWkst = ActiveSheet


LastRow = oWkst.Range("A" & oWkst.Rows.Count).End(xlUp).Row
LastCol = oWkst.Cells(2, oWkst.Columns.Count).End(xlToLeft).Column

For Each oRng In Range(Cells(2, 11), Cells(2, LastCol))
        If oRng.HasFormula Then
            oRng.Copy
            Range(Cells(2, StartCol), Cells(LastRow, [CURRENT COLUMN])).PasteSpecial (xlPasteFormulas)
        End If
        Next oRng

End Sub

Try to modify 尝试修改

Range(Cells(2, StartCol), Cells(LastRow, [CURRENT COLUMN])).PasteSpecial (xlPasteFormulas

to

Range(Cells(2, oRng.Column), Cells(LastRow, oRng.Column)).PasteSpecial (xlPasteFormulas)

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

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