使用Macro Statement添加填充的行

时间:2013-07-12 14:22:35

标签: excel vba

这是我目前正在使用的声明。我需要找到一种方法来填充我插入的两行,使用新空行之前的最后一个填充单元格的值。我该怎么做?

Sub Insert_Rows()
Dim r As Long, mcol As String, i As Long, s As Long, ncol As Long

' find last used cell in Column A
  r = Cells(Rows.Count, "A").End(xlUp).Row

 ' get value of  last used cell in column A
  mcol = Cells(r, 1).Value

'find last used cell in Column B
 s = Cells(Rows.Count, "B").End(xlUp).Row

  ' get value of last used cell in Column B
  ncol = Cells(s, 1).Value

 ' insert rows by looping from bottom
  For i = r To 2 Step -1
     If Cells(i, 1).Value <> mcol Then
       mcol = Cells(i, 1).Value
        Rows(i + 1).Insert
    Rows(i + 1).Insert
     End If
  Next i
End Sub

1 个答案:

答案 0 :(得分:0)

插入行后,您可以使用:

Rows(i + 1).Cells(1, 1).Value = "hi.."    'or
Rows(i + 1).Cells(1, 1).Value = mcol
Rows(i + 2).Cells(1, 1).Value = "there"

将值插入这些行的第一个单元格中。