2013-10-09 24 views
2

我想创建一个Excel宏,执行下列操作复制公式:输入一个新的生产线,并从电池单元的上方

  1. 在文件末尾输入新行

  2. 复制公式从上面

细胞到目前为止,我有这样的:

Sub New_Delta() 

    ' Go to last cell 
    Range("A4").Select 
    Selection.End(xlDown).Select 
    LastCell = [A65536].End(xlUp).Offset(-1, 0).Address 
    Range(LastCell).Select 

    ' Enter new line 
    Selection.EntireRow.Insert Shift:=xlUp, CopyOrigin:=xlFormatFromLeftOrAbove 

    ' Copy formula from cell above 
    Dim oCell As Range 
     For Each oCell In Selection 
      If (oCell.Value = "") Then 
      oCell.Offset(-1, 0).Copy Destination:=oCell 
      End If 
     Next oCell 

End Sub 

这会将公式用于第一小区“A”,但不是下述的

我想要做的事像Selection.Offset(0, 1).Select,然后遍历,多达“K”(优选地不“G”和“H” )

但我卡住了,真的可以使用一些帮助。

编辑:我想是这样的(非工作伪代码)

' Copy formula from cell above 
Dim oCell As Range 
     While (oCell.Offset(-1, 0).Value != "") ' If the cell above is not empty 
     oCell.Offset(-1, 0).Copy Destination:=oCell ' Copy the formula from the cell above 
     Selection.Offset(0, 1).Select ' Move one cell to the right 
+0

为什么你会插入新行如果下一个空行已经是空的?列是不同的长度?这么做的意义何在?对不起,但它不清楚你在做什么 – 2013-10-09 11:20:40

+0

可能从上面的行('xlFormatFromLeftOrAbove')获取格式。 –

+0

是什么你需要什么插入>表>表自动执行? – pnuts

回答

5

你可以简单地复制/插入行之前插入新行

Sub New_Delta() 

    ' Go to last cell 
    Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select 

    ' Copy formula from cell above 
    Rows(Selection.Row - 1).Copy 
    Rows(Selection.Row).Insert Shift:=xlDown 

End Sub 
+1

我过去这样做了很多。 谢谢。 –

+1

我看的时候代码越来越简单。 –

相关问题