office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

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

Excel vba在特定的位置插入行併自動添加值

2019-11-20 14:50:00
youngmiffy
轉貼
12184

在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



運行函數,在行中插入日期成功

    分享