MatchCase:=False).row 和 "run-time error 91"

MatchCase:=False).row and "run-time error 91"

我想在 commandbutton 中输入一些文本和值并将其填充到电子表格中。我的代码在这里:

Private Sub CommandButton1_Click()

Dim rng As Range
Dim LastRow As Long

Set rng = ActiveSheet.ListObjects("table1").Range


LastRow = rng.Find(What:=” * ”, _
    After:=rng.Cells(1), _
    Lookat:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Row

    rng.Parent.Cells(LastRow + 1, 1).Value = TextBox1.Text
    rng.Parent.Cells(LastRow + 1, 2).Value = ComboBox1.Text
    rng.Parent.Cells(LastRow + 1, 3).Value = TextBox3.Value
    rng.Parent.Cells(LastRow + 1, 4).Value = TextBox4.Value
    rng.Parent.Cells(LastRow + 1, 5).Value = TextBox5.Value

End Sub

我在 matchcase:=false).row 上不断收到 运行 时间错误 91,我不知道为什么。多一双眼睛会很有帮助。

您似乎在向现有 table (ListObject) 添加一行。无需查找最后一行 - 您只需使用 ListRows.Add。如果您不指定要添加行的位置,它会自动添加在底部。

这会简化您要执行的操作。

Private Sub CommandButton1_Click()
    Dim newRow As ListRow
    Dim tbl As ListObject
    Set tbl = ActiveSheet.ListObjects("table1")
    Set newRow = tbl.ListRows.Add

    With newRow
        .Range(1) = Textbox1.Text
        .Range(2) = ComboBox1.Text
        .Range(3) = TextBox3.Value
        .Range(4) = TextBox4.Value
        .Range(5) = TextBox5.Value
    End With
End Sub