MatchCase:= False).row和"运行时错误91"

时间:2018-05-31 18:06:55

标签: excel vba excel-vba commandbutton

我想在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,我不知道为什么。额外的一双眼睛会非常有帮助。

1 个答案:

答案 0 :(得分:0)

您似乎正在向现有表格添加一行(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