第二个实例的Excel错误

时间:2017-09-28 01:26:11

标签: excel-vba runtime-error vba excel

我希望有人可以帮我解决这个问题。我不知道什么是错的,但是我的程序给了我一个错误说明:

lr.Range(1, 1).Value = Date

这一行代码指向:

Dim ws As Worksheet
Dim ws1 As Worksheet
Dim lo As ListObject
Dim lr As ListRow
Dim X As String


Set ws1 = Sheets("ACCOUNTS")
Set ws = Sheets("OB RECORDS")


ws1.Activate
ws1.Range("B1").Select

Do Until ActiveCell.Value = Me.TextBox1.Text
ActiveCell.Offset(1, 0).Select
If IsEmpty(ActiveCell.Value) Then
MsgBox "No Time-In Found", vbCritical
Me.TextBox1.Text = ""
Me.TextBox1.SetFocus
Exit Sub
End If
Loop

ActiveCell.Offset(0, -1).Select
X = ActiveCell.Value
ws.Activate
ws.Range("E6").Select

Do Until X = Me.txtcont.Text 'txtcont value is being populated at a different form (frmpass.txtcont.Text = Me.cmbstaff.Text)

ActiveCell.Offset(1, 0).Select

If IsEmpty(ActiveCell.Value) Then

MsgBox "No account matches this password. Please try again.", vbCritical
Me.TextBox1.Text = ""
Me.TextBox1.SetFocus
Exit Do
Exit Sub

End If
Loop

Range("I" & (ActiveCell.Row)).Select
If IsEmpty(ActiveCell) Then

MsgBox "Unable to Time-Out this time because you still have an unclosed O.B filed." & vbNewLine & "Please Time-In and close first.", vbCritical

Unload Me
frmOB.Show

Exit Sub
End If

Set lo = ws.ListObjects(1)
Set lr = lo.ListRows.Add

ws.Unprotect "321321"

lr.Range(1, 1).Value = Date 'this is where the error is and I don't know why
lr.Range(1, 2).Value = frmout.TextBox1.Text
lr.Range(1, 3).Value = UCase(frmout.cmbstaff.Text)
lr.Range(1, 4).Value = UCase(frmout.TextBox2.Text)
lr.Range(1, 5).Value = UCase(frmout.TextBox3.Text)
lr.Range(1, 5).Value = UCase(frmout.TextBox3.Text)
lr.Range(1, 6).Value = Time
lr.Range(1, 6).NumberFormat = "h:mm:ss AM/PM"
lr.Range(1, 8).Value = UCase(frmout.TextBox4.Text)
ActiveWorkbook.Save
MsgBox "O.B log successfully recorded. Take care!", vbInformation
ws.Protect "321321"

Unload Me

frmOB.Show

Exit Sub

Application.ScreenUpdating = True

我的程序适用于第一个实例,(第一次运行它。)然后在重复此过程时抛出此错误。请帮忙!我被困在这里一个星期。

这里是放在命令按钮上的完整代码。基本上代码所做的是记录用户从不同用户表单输入的几个细节。

{{1}}

1 个答案:

答案 0 :(得分:0)

在以下代码中,您尝试在受保护的工作表中添加listrow:

Set lo = ws.ListObjects(1)
Set lr = lo.ListRows.Add

ws.Unprotect "321321"

所以你的add方法不起作用,lr什么都没有。当您尝试使用lr.range时,excel会抛出错误。

在添加新行之前,只需放置unprotect:

ws.Unprotect "321321"

Set lo = ws.ListObjects(1)
Set lr = lo.ListRows.Add