【问题标题】:UserForm Run-time-error | Application-defined or object defined error用户窗体运行时错误 |应用程序定义或对象定义的错误
【发布时间】:2019-08-13 05:08:47
【问题描述】:

我用 Excel 制作了一个用户窗体。它有文本框和复选框以及命令按钮。 UserForm 以 TextBoxes 开头,然后出现一些 CheckBoxes 和 TextBoxes 继续。 cmdAddData_click 用于将数据发送到工作表。我搜索了很多,但我是这里的新手。

当我想发送数据时它会给出

运行时错误、应用程序定义或对象定义错误。

感谢所有帮助。

Private Sub cmdAddData_Click()
lastrow = ThisWorkbook.Worksheets("00. Active Customers").Cells(Rows.Count, 1).End(xlUp).Row

ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 0).Value = txtDate.Text
ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 1).Value = txtName.Text

'after 34 textboxes, now for checkboxes

If cbxADSL.Value = True Then
ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 35).Value = "Yes"
Else
ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 35).Value = "No"
End If


If cbxAlarm.Value = True Then
ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 36).Value = "Yes"
Else
ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 36).Value = "No"
End If


'after checkboxes, now for textboxes

ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 122).Value = txtFirstContact.Text
ThisWorkbook.Worksheets("00. Active Customers").Cells(lastrow + 1, 123).Value = txtLastContact.Text


End Sub

【问题讨论】:

  • 你在哪一行得到错误?
  • 嘿:)。在以 ThisWorkbook.Worksheets("00. Active Customers") 开头的第三行......
  • 啊,当然:CellsCells(row, column) 一样使用,但你写的是Cells(lastrow + 1, 0)。问题是列编号以1 开头,而0 列不存在。 • 此外,我建议将With statement 用于With ThisWorkbook.Worksheets("00. Active Customers")。所以你不必一遍又一遍地重复。
  • 非常感谢伙计!有效!但我不明白其他建议:)
  • 作为答案发布。

标签: excel vba checkbox listbox userform


【解决方案1】:

问题是Cells(lastrow + 1, 0)

CellsCells(row, column) 一样使用,但列编号以 1 开头,并且列 0 不存在。

此外,您可以使用With StatementIIf function 减少代码:

Private Sub cmdAddData_Click()
    With ThisWorkbook.Worksheets("00. Active Customers")
        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row

        .Cells(lastrow + 1, 0).Value = txtDate.Text '<-- column number must be fixed
        .Cells(lastrow + 1, 1).Value = txtName.Text

        'after 34 textboxes, now for checkboxes
        .Cells(lastrow + 1, 35).Value = IIf(cbxADSL.Value, "Yes", "No")
        .Cells(lastrow + 1, 36).Value = IIf(cbxAlarm.Value, "Yes", "No")    

        'after checkboxes, now for textboxes
        .Cells(lastrow + 1, 122).Value = txtFirstContact.Text
        .Cells(lastrow + 1, 123).Value = txtLastContact.Text
    End With
End Sub

【讨论】:

    猜你喜欢
    • 2019-05-14
    • 2023-03-28
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多