VBA将记录添加到工作表

时间:2019-04-11 19:20:15

标签: excel vba

默认情况下,当我单击添加命令按钮时,它将添加到工作表的第三行。应该将其添加到第一行。我需要在VBA代码或按钮属性中进行更改吗?

Private Sub CmdAdd_Click()
    Dim wks As Worksheet
    Dim AddNew As Range
    Set wks = Sheet1
    Set AddNew = wks.Range("AA1000000").End(xlup).Offset(1, 0)
    AddNew.Offset(0, 0).Value = txtcustomerid.Text
    AddNew.Offset(0, 1).Value = txtContract.Text
    AddNew.Offset(0, 2).Value = txtContractLBS.Text
    AddNew.Offset(0, 3).Value = txtContractPrice.Text
    AddNew.Offset(0, 4).Value = txtItem.Text
    AddNew.Offset(0, 5).Value = Txtitemnum.Text
    AddNew.Offset(0, 6).Value = TxtCustomerName.Text
    AddNew.Offset(0, 7).Value = Txtstartdate.Text
    AddNew.Offset(0, 8).Value = Txtenddate.Text
    AddNew.Offset(0, 9).Value = TxtSalesPerson.Text
    AddNew.Offset(0, 10).Value = TxtBroker.Text
    AddNew.Offset(0, 11).Value = TxtTerms.Text
    End Sub

1 个答案:

答案 0 :(得分:1)

考虑使用ActiveSheet.UsedRange,请参阅: https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba

我怀疑您当前的问题是由于格式数据存在,还是AA列中的数据不足-这可能是您的代码中的错字了?