【问题标题】:insert row in a specific position在特定位置插入行
【发布时间】:2020-07-08 20:25:06
【问题描述】:

我有这个在 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

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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    如果你有一个单元格引用并且你想在它之前插入一行 cell.EntireRow.Insert` 类似于:

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

    ...或者如果您想在第 3 行之前插入:

    Rows(3).EntireRow.Insert
    

    ...或插入多行:

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

    更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-08
      • 2021-05-01
      • 1970-01-01
      • 2012-05-21
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多