office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Excel vba在特定的位置插入行并自动添加值

2019-11-20 14:50:00
youngmiffy
转贴
12207

在Excel数据表中, 我们可能因为某些原因删除了部分数据,或者漏输入了数据。

但是数据是有顺序的,而这部分数据是在数据表中间的任何位置。如果手动插入再录入数据就会当然麻烦

如下图中,某些月份被删除了。希望间隔插入所有月份


B列是辅助列,填写需要插入的行数。变量“intRow”是获取表格的行数,变量“intFirst”是插入第一行的位置

Sub AddRow()
Dim intFirstValue As Integer, intFirst As Integer, intRow As Integer, n As Integer
   intRow = Sheet8.Range("a65536").End(xlUp).Row
    intFirst = 3
    For n = 0 To intRow
     If Cells(intFirst - 1, "B").Value <> 0 Then
        Rows(intFirst).Resize(Cells(intFirst - 1, "B").Value).Insert
        For intFirstValue = 1 To Cells(intFirst - 1, "B").Value
          Cells(intFirst, "A").Value = DateAdd("m", 1, Cells(intFirst - 1, "A").Value)
          intFirst = intFirst + 1
        Next
    End If
    intFirst = intFirst + Cells(intFirst - 1, "B").Value + 1
    Next n
End Sub



运行函数,在行中插入日期成功

    分享