Excel用户窗体偶尔不会将数据输入电子表格

时间:2019-01-09 15:00:27

标签: excel vba userform

我在Excel VBA中创建的用于将数据输入数据库的用户窗体可以工作,但是有时我的用户报告说,他们使用该窗体输入的数据没有填充在电子表格中。我一般都不擅长使用Excel VBA进行编码和编程,因此我不确定解决此类问题的最佳方法。

请注意,此问题是间歇性的,因此我不知道这是用户正在执行还是未执行的操作。

我本来以为由于“在数据库部分找到第一个空行”的错误而导致数据可能填充在电子表格的底部,但是我在那里找不到任何数据。

Private Sub CommandButton1_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("QA Data")

'find first empty row in database
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for lookup code
If Trim(Me.txtLookup_Code.Value) = "" Then
    Me.txtLookup_Code.SetFocus
    MsgBox "Please enter a Lookup Code"
    Exit Sub
End If

'copy the data to the database
'use protect and unprotect lines,
'   with your password
'   if worksheet is protected
With ws
'   .Unprotect Password:="password"
    .Cells(lRow, 1).Value = Me.txtLookup_Code.Value
    .Cells(lRow, 2).Value = Me.cboCM_Name.Value
    .Cells(lRow, 3).Value = Me.txtCM_Errors.Value
    .Cells(lRow, 4).Value = Me.cboSP_Site.Value
    .Cells(lRow, 6).Value = Me.cboChecklist_Type.Value
    .Cells(lRow, 7).Value = Me.txtActivity_Description.Value
    .Cells(lRow, 8).Value = Me.cboProcessor_Name.Value
    .Cells(lRow, 9).Value = Me.txtCritical_Errors.Value
    .Cells(lRow, 10).Value = Me.txtNon_Critical_Errors.Value
    .Cells(lRow, 12).Value = Date
    .Cells(lRow, 13).Value = Application.UserName
'   .Protect Password:="password"
End With

'clear the data
Me.txtLookup_Code.Value = ""
Me.cboCM_Name.Value = ""
Me.txtCM_Errors.Value = ""
Me.cboSP_Site.Value = ""
Me.cboChecklist_Type.Value = ""
Me.txtActivity_Description.Value = ""
Me.cboProcessor_Name.Value = ""
Me.txtCritical_Errors.Value = ""
Me.txtNon_Critical_Errors.Value = ""
Me.txtLookup_Code.SetFocus

End Sub

Private Sub UserForm_Initialize()
Dim cName As Range
Dim cSite As Range
Dim cCheck As Range
Dim cProcessor As Range
Set ws = Worksheets("Lists")

For Each cName In ws.Range("CMName")
  With Me.cboCM_Name
    .AddItem cName.Value
    .List(.ListCount - 1, 1) = cName.Offset(0, 1).Value
  End With
Next cName

For Each cSite In ws.Range("SPSite")
    With Me.cboSP_Site
        .AddItem cSite.Value
        .List(.ListCount - 1, 1) = cSite.Offset(0, 1).Value
    End With
Next cSite

For Each cCheck In ws.Range("ChecklistType")
    With Me.cboChecklist_Type
        .AddItem cCheck.Value
        .List(.ListCount - 1, 1) = cCheck.Offset(0, 1).Value
    End With
Next cCheck

For Each cProcessor In ws.Range("ProcessorName")
    With Me.cboProcessor_Name
        .AddItem cProcessor.Value
        .List(.ListCount - 1, 1) = cProcessor.Offset(0, 1).Value
    End With
Next cProcessor

Me.txtLookup_Code.SetFocus

End Sub

我希望电子表格填充有按下“添加”按钮时输入的数据,但是偶尔也行不通。

1 个答案:

答案 0 :(得分:0)

线

lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
不仅会找到一个值,该值不仅位于最后一行,而且位于该行的最后一列。

您可以想象它从ws.Range($XFD$1048576)ws.Range("$A$1")从右到左搜索,然后向上移动。因此,如果偶然,在ws.Range("$AA$57)处存在一个值,您的输出将最终排在第58行。

一种更一致的方法来找到用于目的的最后一行-因为您的目标是依次向1-13列中输入数据-因此可以让lRow = ws.Range("A" & Rows.Count).End(xlup).Row + 1。 使用这种方法,您只需在A列中搜索最后一个填充的行,然后将其中一个向下移动到下一个空行。 为了进一步消除异常值错误,set ws = ThisWorkbook.Worksheets("QA Data")可能是您的最大利益。

关于调试此类问题的建议,您可以创建一个额外的工作表,在本示例中,我们将其称为“错误”,然后在CommandButton1_Click()的代码末尾添加< / p>

With ThisWorkbook.Worksheets("Error")
    .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row + 1) = "Data Entry Point @ Row " & lRow
End With

仅查看数据的输出位置。