单击VBA表单button,更改行中所有列的值

我已经为我的新数据库添加了一个帐单的一个美丽的forms。 现在我需要“提交”button来计算和组织我的工作簿中的信息。 为了简化这个问题,我首先关注一个我命名为billData的工作表。 我将需要表单的每个input字段自动填充到billData工作表的下一个可用行。

这是我到目前为止所提出的:

Private Sub NB_SubmitButton_Click() 'NB stands for New Bill Dim OwnerName As String Dim ExpenseName As String Dim CycleValue As String 'such as every day, every week, every month etc. Dim OnValue As String 'such as Monday, the 5th, the 10th, etc. Dim AndValue As String 'such as 19th, 24th, etc. Dim DueAmount As Currency Dim SavedAmount As Currency Dim NoteValue As String Dim x As Long NoteValue = NB_NoteTextBox.Value SavedAmount = NB_SavedAmountTextBox.Value DueAmount = NB_DueAmountTextBox.Value AndValue = NB_AndComboBox.Value OnValue = NB_OnComboBox.Value CycleValue = NB_CycleComboBox.Value ExpenseName = NB_ExpenseNameTextBox.Value OwnerName = NB_OwerNameTextBox.Value x = 1 For x = 1 To Rows.Count If Worksheets("billData").Range(Cells(x, 1)).Value = "" Or _ IsEmpty(Worksheets("billData").Cells(x, 1).Value) Then Worksheets("billData").Range(Cells(x, 2)).Value = OwnerName Worksheets("billData").Range(Cells(x, 4)).Value = ExpenseName Worksheets("billData").Range(Cells(x, 7)).Value = CycleValue Worksheets("billData").Range(Cells(x, 8)).Value = OnValue Worksheets("billData").Range(Cells(x, 9)).Value = DueAmount Worksheets("billData").Range(Cells(x, 10)).Value = SavedAmount Worksheets("billData").Cells(x, 11).Value = NoteValue Else End If Next x 

当我通过立即运行的代码,我不断收到错误消息:

编译错误:下一个没有For

然后,当我运行所有的代码,填写表单上的字段,然后单击“提交”button,我得到这个错误:

运行时错误“9”:下标超出范围

另外,有没有更好的方法来获得我的目标? 我应该使用VLookup吗?

编辑添加屏幕截图:

在这里输入图像说明

 'x = 1 'you don't need this... Dim sht As Worksheet Set sht = Worksheets("Bill Data") For x = 1 To Rows.Count If Len(sht.Cells(x, 1).Value) = 0 Then With sht.Rows(x) '? what goes in ColA ? .Cells(2).Value = OwnerName .Cells(4).Value = ExpenseName .Cells(7).Value = CycleValue .Cells(8).Value = OnValue .Cells(9).Value = DueAmount .Cells(10).Value = SavedAmount .Cells(11).Value = NoteValue End With End If Next x