将数据添加到新行Excel VBA

时间:2017-02-16 02:58:53

标签: excel vba excel-vba

在此代码中,我尝试在购买更多商品时添加商品数据并更新库存水平。我不确定如何将产品数据添加到下一行,但我的nextERow正在返回' 0'而不是下一个空行值。无论如何我能解决这个问题吗?

Private Sub btnAdd_Click()
With Worksheets("Inventory List")
    'update quantity if there is an existing product
    If .Cells(nextERow, 2).Value = nameTextBox.Text Then
        .Cells(nextERow, 3).Value = .Cells(nextERow, 3).Value + quantityTextBox.Value

    'insert product on a new row if current row is not empty
    ElseIf Len(.Cells(nextERow, 2).Value) > 0 Then
        .Cells(nextERow, 2).Value = nameTextBox.Text
        .Cells(nextERow, 3).Value = quantityTextBox.Value
        .Cells(nextERow, 4).Value = priceTextBox.Text

    'insert product if current row is empty
    ElseIf IsEmpty(.Cells(nextERow, 2)) Then
        .Cells(nextERow, 2).Value = nameTextBox.Text
        .Cells(nextERow, 3).Value = quantityTextBox.Value
        .Cells(nextERow, 4).Value = priceTextBox.Text
    End If
End With
End Sub

Function nextERow() As Integer
    nextERow = Worksheets("Inventory List").Cells(Rows.Count,1).End(xlUp).Offset(1, 0).row
End Function

0 个答案:

没有答案