代码之家  ›  专栏  ›  技术社区  ›  Lelio Faieta

在特定位置插入行

  •  1
  • Lelio Faieta  · 技术社区  · 7 年前

    我有一个将新行插入Excel工作表的子表。 每一行在A列中都有一个日期,我希望将日期按顺序(从最早到最新)插入。因此,如果要插入的日期大于最后一个日期,我只需在底部添加一个新行。 否则,我将查找插入空行并填充数据的位置。 我被困在插入新行的部分:

    cell.Rows(riga).EntireRow.insert
    

    整个潜艇是这样的:

    Sub insert_row()
        lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
        data_last = Range("A" & lastRow).Value
    
        'prendo i valori
        data_mov = new_mov.data.Value
        descr_mov = new_mov.descr.Value
        importo_mov = new_mov.importo.Value
    
        'emetto un alert se la data inserita minore di quella della riga precedente (lastRow)
        If data_mov < data_last Then
            MsgBox "movimento nel passato"
            Dim rng As Range, cell As Range
            Set rng = Range("C5:C" & lastRow)
            For Each cell In rng
                riga = cell.Row
                data = Range("A" & riga).Value
                If data > data_mov Then
                    riga = riga - 1
                    cell.Rows(riga).EntireRow.insert
                    Exit For
                End If
                Next cell
    
        Else
            'li inserisco nella prima riga vuota sotto
            nxt_row = lastRow + 1
            Range("A" & nxt_row).Value = data_mov
            Range("B" & nxt_row).Value = descr_mov
            Range("C" & nxt_row).Value = CDbl(importo_mov)
            Call Saldo_upd
        End If
        'evidenzio una cella di quella riga
    
    
    
        'ricalcolo il saldo
    
    End Sub
    

    我没有收到任何错误消息,什么也没有发生。有什么提示吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ashleedawg    7 年前

    dim cell as range
    set cell = Range("A2")
    cell.EntireRow.Insert
    

    Rows(3).EntireRow.Insert
    

    Rows("3:8").EntireRow.Insert
    

    推荐文章