簡體   English   中英

復制並粘貼行和列(帶公式)並粘貼到VBA中的最后一行

[英]Copy and Paste Row and Columns(with formulas) and Paste down to the last row in VBA

首先,我有多張價值和公式的表格。 我已經使用VBA來提取干凈的數據,這是正常的。 例如“Sheet1”,從第3行到第100行,從第H行到第K列是我有值的地方。 現在,我有第3行和B列到F的公式,它鏈接到列H到K的值。我在復制第3行並將公式粘貼到工作表的最后一行時遇到問題。

其次,我有12個工作表。 它們具有不同的值和公式來自“表1”。 但我希望同樣的動作適用於所有12張紙,如“紙張1”。

這是我用來復制和粘貼我的值的代碼(但它只復制一行。我希望它轉到表格的底部):

Sub formula_format (data_sheet As String, row_num_start As Integer, column_num_start As Integer, row_num_end As Integer, column_num_end As Integer)

Sheets(data_sheet).Select

 For Row = row_num_start To row_num_end

    If Cells(Row, column_num_start).Value2 = "" Then

       Range(Cells(Row - 1, column_num_start), Cells(Row - 1, column_num_end)).Copy
       ActiveSheet.Cells(Row, column_num_start).Select
       ActiveCell.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
       ActiveCell.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

       Exit For

    End If

Next Row

End Sub

此代碼復制第一行並將其粘貼到下一行。 我需要繼續寫完最后一行。

提前致謝。 對不起,很長的帖子。 這是我的第一篇文章!

除了上面的評論之外,我還想提供一種替代方法,如果我正確理解你要做的事情,那么這種方法更簡單,更快速,更容易維護(在給定長度的行之間復制公式)。

Sub formula_format(data_sheet As String, row_num_start As Integer, column_num_start As Integer, row_num_end As Integer, column_num_end As Integer)

With Sheets(data_sheet)

    .Range(.Cells(row_num_start, column_num_start), .Cells(row_num_start, column_num_end)).AutoFill _
        Destination:=.Range(.Cells(row_num_start, column_num_start), .Cells(row_num_end, column_num_end))

End With


End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM