运行 将用户表单数据添加到 table 时出现时间错误“91”
Run Time Error '91' When adding userform data to table
我正在尝试将简单数据输入到用户表单中的两个文本框中,然后将此数据添加到名为 "AvantAct" 的 table 中。每次用户表单为 运行 时,我都希望将数据输入到 table 的第一个空白行中。有趣的是,我第一次这样做时它完美无缺。但是,退出工作簿然后稍后再返回后,我似乎得到:-
Run time error '91': Object variable or With block variable not set.
当我调试时,以下行被突出显示:
tbl.DataBodyRange(lrow2, 1). Value = Me.TxtDate.Value
我应该补充一点,我的 table 目前是空的(新插入的)table。它有 8 列(headers),一个空行(从插入开始)和一整行。
有什么建议吗?
Private Sub SubmitButton_Click()
Dim ws As Worksheet
Dim tbl As ListObject
Dim TxtDate As Date
Dim TxtPmt As Currency
Dim col As Integer
Dim lrow As Range
Dim lrow2 As Long
Set ws = ActiveWorkbook.Worksheets("Avant")
Set tbl = ws.ListObjects.Item("AvantAct")
If tbl.ListRows.Count > 0 Then
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
For col = 1 To lrow.Columns.Count
If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
tbl.ListRows.Add
Exit For
End If
Next col
End If
lrow2 = tbl.ListRows.Count
tbl.DataBodyRange(lrow2, 1).Value = Me.TxtDate.Value
tbl.DataBodyRange(lrow2, 3).Value = Me.TxtPmt.Value
Unload Me
End Sub
问题是因为 table 开头是空的。
If tbl.ListRows.Count > 0 Then
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
...
End If
因为计数不大于零(它是零)lrow
从未设置,因此出现错误。
If tbl.ListRows.Count = 0 Then
tbl.ListRows.Add
else
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
...
End If
另外,您的问题是:-
I want the data entered into the first blank row
代码没有这样做,代码只检查最后一行,如果它不为空则添加一行,所以在 5 行中第三行为空的列表中,不会使用第三行,而是在底部添加一行。以下将如您所愿:-
Private Sub SubmitButton_Click()
Dim ws As Worksheet
Dim tbl As ListObject
Dim TxtDate As Date
Dim TxtPmt As Currency
Dim col As Integer
Dim lrow As Range
Dim lrow2 As Long
Dim BlnYesNo As Boolean
Set ws = ActiveWorkbook.Worksheets("Avant")
Set tbl = ws.ListObjects.Item("AvantAct")
If tbl.ListRows.Count = 0 Then
tbl.ListRows.Add
lrow2 = 1
Else
For lrow2 = 1 To tbl.ListRows.Count
Set lrow = tbl.ListRows(lrow2).Range
'If it stays true, we must add a row
BlnYesNo = True
For col = 1 To lrow.Columns.Count
If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
BlnYesNo = False
Exit For
End If
Next
If BlnYesNo Then Exit For
Set lrow = Nothing
Next
'If its false then all rows had data and we need to add a row
If Not BlnYesNo Then
tbl.ListRows.Add
lrow2 = tbl.ListRows.Count
End If
End If
tbl.DataBodyRange(lrow2, 1).Value = "A"
tbl.DataBodyRange(lrow2, 3).Value = "B"
Set tbl = Nothing
Set ws = Nothing
End Sub
我正在尝试将简单数据输入到用户表单中的两个文本框中,然后将此数据添加到名为 "AvantAct" 的 table 中。每次用户表单为 运行 时,我都希望将数据输入到 table 的第一个空白行中。有趣的是,我第一次这样做时它完美无缺。但是,退出工作簿然后稍后再返回后,我似乎得到:-
Run time error '91': Object variable or With block variable not set.
当我调试时,以下行被突出显示:
tbl.DataBodyRange(lrow2, 1). Value = Me.TxtDate.Value
我应该补充一点,我的 table 目前是空的(新插入的)table。它有 8 列(headers),一个空行(从插入开始)和一整行。
有什么建议吗?
Private Sub SubmitButton_Click()
Dim ws As Worksheet
Dim tbl As ListObject
Dim TxtDate As Date
Dim TxtPmt As Currency
Dim col As Integer
Dim lrow As Range
Dim lrow2 As Long
Set ws = ActiveWorkbook.Worksheets("Avant")
Set tbl = ws.ListObjects.Item("AvantAct")
If tbl.ListRows.Count > 0 Then
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
For col = 1 To lrow.Columns.Count
If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
tbl.ListRows.Add
Exit For
End If
Next col
End If
lrow2 = tbl.ListRows.Count
tbl.DataBodyRange(lrow2, 1).Value = Me.TxtDate.Value
tbl.DataBodyRange(lrow2, 3).Value = Me.TxtPmt.Value
Unload Me
End Sub
问题是因为 table 开头是空的。
If tbl.ListRows.Count > 0 Then
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
...
End If
因为计数不大于零(它是零)lrow
从未设置,因此出现错误。
If tbl.ListRows.Count = 0 Then
tbl.ListRows.Add
else
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
...
End If
另外,您的问题是:-
I want the data entered into the first blank row
代码没有这样做,代码只检查最后一行,如果它不为空则添加一行,所以在 5 行中第三行为空的列表中,不会使用第三行,而是在底部添加一行。以下将如您所愿:-
Private Sub SubmitButton_Click()
Dim ws As Worksheet
Dim tbl As ListObject
Dim TxtDate As Date
Dim TxtPmt As Currency
Dim col As Integer
Dim lrow As Range
Dim lrow2 As Long
Dim BlnYesNo As Boolean
Set ws = ActiveWorkbook.Worksheets("Avant")
Set tbl = ws.ListObjects.Item("AvantAct")
If tbl.ListRows.Count = 0 Then
tbl.ListRows.Add
lrow2 = 1
Else
For lrow2 = 1 To tbl.ListRows.Count
Set lrow = tbl.ListRows(lrow2).Range
'If it stays true, we must add a row
BlnYesNo = True
For col = 1 To lrow.Columns.Count
If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
BlnYesNo = False
Exit For
End If
Next
If BlnYesNo Then Exit For
Set lrow = Nothing
Next
'If its false then all rows had data and we need to add a row
If Not BlnYesNo Then
tbl.ListRows.Add
lrow2 = tbl.ListRows.Count
End If
End If
tbl.DataBodyRange(lrow2, 1).Value = "A"
tbl.DataBodyRange(lrow2, 3).Value = "B"
Set tbl = Nothing
Set ws = Nothing
End Sub