插入新行后如何自动增加数字?

时间:2019-07-17 09:51:49

标签: excel vba

在数据表中,每当添加新信息时,我都试图插入新行。 每行都有一个唯一的索引,例如在第一列中,我有一些工作域,例如“领航”,在第二列中,我有该域的子域,例如,对于“领航员”,我具有“质量”,索引是Pil_01。每个子域都有其自己的产品,当我在位置15添加产品时,我希望属于该域领航的第15行以下的索引增加1

Private Sub CommandButton2_Click()    
    Dim position As Integer
    Dim dom As String
    Dim tbl As ListObject
    Dim ws As Worksheet
    Dim newrow As ListRow

    ' domain is the name of the comobox in which the user will choose the domain and subdomain the name of the comobox in which the user will choose the sub domain\ domsubdomain is a column in my table which has the 3 first letters of the domain concatenated with the subdomain
    dom = Left(domain, 3) & subdomain
    position = Application.WorksheetFunction.Match(dom, Sheets("Data").Range("domsubdomain"), 0)

    ' this is the code allowing me to insert a new row and add some data into it
    Set ws = Sheets("Data")
    Set tbl = ws.ListObjects("Referentiel")
    Set newrow = tbl.ListRows.Add(position + 1)

    With newrow
        .Range(2) = subdomain
        .Range(3) = Produit
        .Range(4) = Procedure
    If Procedure <> "" Then
        count = 1
        .Range(5) = Processus
    End If


    ' The for loop will allow me to go through all data in that column starting from the new row's position and increment the cells by one. It stops when meeting a new domain
    For i = position To 1000
        If dom = Sheets("Data").Range("domsubdomain").Cells(i, 25).Value Then
            chaine = Sheets("Data").Range("domsubdomain").Cells(i, 24).Value
            chaine = chaine + 1
            Sheets("Data").Range("domsubdomain").Cells(i, 24).Value = chaine
        Else
            MsgBox "Exit"
            Exit For
        End If
    Next i  
End Sub

使用此代码,我将退出消息,而不是通过下面的代码

0 个答案:

没有答案