Excel 将公式插入空行

Excel 将公式插入空行,excel,vba,Excel,Vba,我有VBA代码来插入用户定义数量的空行,它工作得很好。我需要添加到代码中的是一种在插入公式后在第一个空行中插入公式的方法。具体地说,我需要将J+T、K+U、L+V、M+W、N+T、O+U、P+W、Q+U、R+W和S+V列中最后一行数据中的值添加到插入的每个空行中 如果有帮助,插入空白行的VBA代码如下: Dim NumRowsToInsert, FirstRow As Long Dim RowIncrement As Long Dim ws As Excel.Worksheet Dim Last

我有VBA代码来插入用户定义数量的空行,它工作得很好。我需要添加到代码中的是一种在插入公式后在第一个空行中插入公式的方法。具体地说,我需要将J+T、K+U、L+V、M+W、N+T、O+U、P+W、Q+U、R+W和S+V列中最后一行数据中的值添加到插入的每个空行中

如果有帮助,插入空白行的VBA代码如下:

Dim NumRowsToInsert, FirstRow As Long
Dim RowIncrement As Long
Dim ws As Excel.Worksheet
Dim LastRow As Long
Dim LastEvenlyDivisibleRow
Dim i As Long

Do
FirstRow = InputBox("Please indicate at which row you want to start.")
Loop Until IsNumeric(FirstRow) 
NumRowsToInsert = InputBox("How many rows would you like to insert _
between each row of data?")
Do
Loop Until IsNumeric(NumRowsToInsert)
RowIncrement = InputBox("How many rows of data between line inserts?")
Do
Loop Until IsNumeric(RowIncrement)
Set ws = ActiveSheet
With ws
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
LastEvenlyDivisibleRow = Int(LastRow / RowIncrement) * RowIncrement
If LastEvenlyDivisibleRow = 0 Then
    Exit Sub
End If
Application.ScreenUpdating = False
For i = LastEvenlyDivisibleRow To FirstRow Step -RowIncrement
    .Range(i & ":" & i + (NumRowsToInsert - 1)).Insert xlShiftDown
Next i
End With
Application.ScreenUpdating = True
End Sub

非常感谢您的帮助

要在单元格中添加公式,您需要做一些相应的操作

'this makes the value of Row 20 Column 1 the sum of B2 through B10
Sheets(1).Cells(20, 1).Formula = "=Sum(B2:B10)"

你能发布你的代码来尝试插入公式吗?我可能在我最初的帖子中解释得不够好。上面的VBA代码所做的是创建由空行(有时是多个空行)分隔的多行数据。我需要VBA代码做的是在每行数据之后的第一个空行中插入公式。上面列出了我需要添加的列。我希望这能更好地描述我正在努力实现的目标。