首页 > 解决方案 > 添加项目后VBA删除行

问题描述

我希望删除添加的项目下方的行,因为一旦我单击确认销售并想添加更多项目。添加的项目将直接替换“总计”、“gst”等行。我可以知道我该怎么做吗?

截屏

这是我添加购物车的代码:

Sub addcart()
Dim lastrow As Long
Dim i As Integer, j As Integer, k As Integer, m As Integer

lastrow = Sheets("Sales Point").Range("E" & Rows.Count).End(xlUp).Row

    If Range("C6").Value = 0 = False And IsEmpty(Range("C6").Value) = False Then
       
        'Number of item
        For i = 5 To lastrow + 1
        Cells(i, 5).Value = i - 4
        Next
        
        Cells(lastrow + 1, 6) = Range("C4") 'Item code
        Cells(lastrow + 1, 7) = Range("C3") 'Item Name
        Cells(lastrow + 1, 8) = Range("C6") 'Quantity
        Cells(lastrow + 1, 9) = Format(Range("C5"), "$#,##.00#") 'Unit Price
        Cells(lastrow + 1, 10) = Format(Cells(lastrow + 1, 9) * Cells(lastrow + 1, 8), "$#,##.00#") 'Total
        
        
    Else
    MsgBox "Error Message!!!"
    
    End If
End Sub

标签: excelvba

解决方案


那这个呢:

Range("A" & (LastRow + 1)).Offset(1,0).EntireRow.Delete

解释:

  • 我相信最后一行等于LastRow + 1
  • Offset(1,0)是下一行的单元格。
  • EntireRow.Delete很明显。

推荐阅读