将公式插入空行

时间:2013-10-21 18:04:27

标签: excel-vba vba excel

我有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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

要向单元格添加公式,您可以执行此操作

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