office交流網(wǎng)--QQ交流群號及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

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

微信交流群(請用微信掃碼)

        

Excel vba在特定的位置插入行并自動(dòng)添加值

2019-11-20 14:50:00
youngmiffy
轉(zhuǎn)貼
13090

在Excel數(shù)據(jù)表中, 我們可能因?yàn)槟承┰騽h除了部分?jǐn)?shù)據(jù),或者漏輸入了數(shù)據(jù)。

但是數(shù)據(jù)是有順序的,而這部分?jǐn)?shù)據(jù)是在數(shù)據(jù)表中間的任何位置。如果手動(dòng)插入再錄入數(shù)據(jù)就會當(dāng)然麻煩

如下圖中,某些月份被刪除了。希望間隔插入所有月份


B列是輔助列,填寫需要插入的行數(shù)。變量“intRow”是獲取表格的行數(shù),變量“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



運(yùn)行函數(shù),在行中插入日期成功

    分享
    文章分類
    聯(lián)系我們
    聯(lián)系人: 王先生
    Email: 18449932@qq.com
    QQ: 18449932
    微博: officecn01
    移動(dòng)訪問