复制并粘贴行和列(使用公式)并粘贴到VBA中的最后一行

时间:2022-09-17 11:38:05

Firstly, I have multiple sheets with values and formula's. I have used VBA to pull down clean data, which is working fine. For Example "Sheet1", From Row 3 to 100 and Column H to K is where I have the values. Now, I have a formula on row 3 and Column B to F that is linked to the values on Columns H to K. I am having problem copying row 3 and pasting the formulas down to the last row on the sheet.

首先,我有多张带有值和公式的表格。我已经使用VBA来提取干净的数据,这是正常的。例如“Sheet1”,从第3行到第100行,从第H行到第K列是我有值的地方。现在,我有第3行和B列到F的公式,它链接到列H到K的值。我在复制第3行并将公式粘贴到工作表的最后一行时遇到问题。

Secondly, I have 12 worksheets. They have different values and formula's from "sheet 1". But I want the same action to apply to all 12 sheets like "sheet 1".

其次,我有12个工作表。它们具有不同的值和公式来自“表1”。但我希望同样的动作适用于所有12张纸,如“纸张1”。

Here is the code I using to copy and paste my values ( but it copies only one row. I want it to go to bottom of sheet) :

这是我用来复制和粘贴我的值的代码(但它只复制一行。我希望它转到工作表的底部):

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

This code copy the first line and pastes it to the next line. I need to continue to the last line of sheet.

此代码复制第一行并将其粘贴到下一行。我需要继续写完最后一行。

Thanks in advance. Sorry for the long post. Its my first post!

提前致谢。对不起,很长的帖子。这是我的第一篇文章!

1 个解决方案

#1


4  

In addition to my comment above, I would like to offer an alternative method, which is much simpler, faster and easier to maintain, if I understand correctly what you are trying to do (copy formulas across columns down a given length of rows).

除了上面的评论之外,我还想提供一种替代方法,如果我正确理解你要做的事情,那么这种方法更简单,更快速,更容易维护(在给定长度的行中复制公式)。

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

#1


4  

In addition to my comment above, I would like to offer an alternative method, which is much simpler, faster and easier to maintain, if I understand correctly what you are trying to do (copy formulas across columns down a given length of rows).

除了上面的评论之外,我还想提供一种替代方法,如果我正确理解你要做的事情,那么这种方法更简单,更快速,更容易维护(在给定长度的行中复制公式)。

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