Excel代码,我的“添加数据”button不起作用

我是编码新手,用googlesearch得到了这个。 我希望这是一个相当简单的代码更改。 我有一个表格,显示我想要的。 我的closuresbutton工作,但是我的“添加数据”button不起作用。 因为它不是填写我在excel中填入完整表格到工作表的信息。 无论我input什么内容,我都会得到总是填充0.1的第一列。如果有人愿意协助,我的代码如下。 我会很感激。

Private Sub cmdbutton_add_Click() Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("Inputs") 'find first empty row in database iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 'Check for a Name number If Trim(Me.textbox_lineno.Value) = " " Then Me.textbox_lineno.SetFocus MsgBox "Please Complete the form" Exit Sub End If 'copy the data to the database ws.Cells(iRow, 1).Value = Me.textbox_lineno.Value ws.Cells(iRow, 1).Value = Me.listbox_pipetype.Value ws.Cells(iRow, 1).Value = Me.textbox_frompit.Value ws.Cells(iRow, 1).Value = Me.textbox_topitormh.Value ws.Cells(iRow, 1).Value = Me.textbox_linealm.Value ws.Cells(iRow, 1).Value = Me.textbox_depthstart.Value ws.Cells(iRow, 1).Value = Me.textbox_depthend.Value ws.Cells(iRow, 1).Value = Me.textbox_pipedia.Value ws.Cells(iRow, 1).Value = Me.textbox_beddingbelow.Value ws.Cells(iRow, 1).Value = Me.textbox_beddingabove.Value ws.Cells(iRow, 1).Value = Me.textbox_roadallowance.Value MsgBox "Data Added", vbOKOnly + vbInformation, "Data Added" 'Clear the data Me.textbox_lineno.Value = "" Me.listbox_pipetype.Value = "" Me.textbox_frompit.Value = "" Me.textbox_topitormh.Value = "" Me.textbox_linealm.Value = "" Me.textbox_depthstart.Value = "" Me.textbox_depthend.Value = "" Me.textbox_pipedia.Value = "" Me.textbox_beddingbelow.Value = "" Me.textbox_beddingabove.Value = "" Me.textbox_roadallowance.Value = "" Me.textbox_lineno.SetFocus End Sub Private Sub cmdbutton_close_Click() Unload Me End Sub 

这行不应该validation:

If Trim(Me.textbox_lineno.Value) = " " Then

你永远不会增加你的专栏作业。 如果我知道一个对象存在,那么我通常会select使用另一个variables的With Object语句。

我将清除文本框的代码提取到它们自己的子例程中。

因为所有的数据都在一行上并且With .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Offset(1)指的是我使用的那一行的第一个单元格Columns(x)类似于使用.Cells(1,x) 。 我看起来很干净

我觉得像这些改变来巩固代码; 使其更易于阅读。 你不必在页面上下滚动,看看子在做什么。


 Private Sub cmdbutton_add_Click() 'Check for a Name number If Trim(Me.textbox_lineno.Value) = "" Then Me.textbox_lineno.SetFocus MsgBox "Please Complete the form", vbInformation, "Action Cancelled" Exit Sub End If With Worksheets("Inputs") With .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Offset(1) 'copy the data to the database .Columns(1).Value = Me.textbox_lineno.Value .Columns(2).Value = Me.listbox_pipetype.Value .Columns(3).Value = Me.textbox_frompit.Value .Columns(4).Value = Me.textbox_topitormh.Value .Columns(5).Value = Me.textbox_linealm.Value .Columns(6).Value = Me.textbox_depthstart.Value .Columns(7).Value = Me.textbox_depthend.Value .Columns(8).Value = Me.textbox_pipedia.Value .Columns(9).Value = Me.textbox_beddingbelow.Value .Columns(10).Value = Me.textbox_beddingabove.Value .Columns(11).Value = Me.textbox_roadallowance.Value End With End With MsgBox "Data Added", vbOKOnly + vbInformation, "Data Added" ClearForm Me.textbox_lineno.SetFocus End Sub Function ClearForm() 'Clear the data Me.textbox_lineno.Value = "" Me.listbox_pipetype.Value = "" Me.textbox_frompit.Value = "" Me.textbox_topitormh.Value = "" Me.textbox_linealm.Value = "" Me.textbox_depthstart.Value = "" Me.textbox_depthend.Value = "" Me.textbox_pipedia.Value = "" Me.textbox_beddingbelow.Value = "" Me.textbox_beddingabove.Value = "" Me.textbox_roadallowance.Value = "" End Function 

在这里输入图像说明