需要解决方案:“抱歉发生错误对象不支持此属性或方法”

时间:2018-02-22 18:52:01

标签: excel vba excel-vba

我的代码使用userform中一组文本框中的信息来查找和编辑两个工作簿中的值。我用来编辑第二个工作簿中的值的代码给出了以下错误,“抱歉错误发生对象不支持此属性或方法”。谁能帮我这个?除了导致错误的原因,我认为我的代码是正确的,但如果有人在我的代码中看到任何错误,请随时纠正我或提供建议。提前致谢!

Private Sub Submit_Click()
Dim WS As Worksheet
Dim lastrow As Long
Dim r As Long
Dim password As String

Application.ScreenUpdating = False
If Not IsNumeric(TextBox1.Text) Then
On Error GoTo ErrorHandler
password = TextBox1.Text
Set WS = ActiveWorkbook.Worksheets("Accounts")
lastrow = WS.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If WS.Cells(r, 2) = Label5.Caption Then
WS.Cells(r, 2).Value = TextBox1.Text
WS.Cells(r, 3).Value = TextBox2.Text
WS.Cells(r, 4).Value = TextBox3.Text
MsgBox "Update Successful", vbInformation
TextBox1.Text = ""
Call Edit_Login
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Enter Correct Information", vbCritical
Application.ScreenUpdating = True
End Sub

Private Sub Edit_Login()
Dim Wkbk As Workbook
Dim txt As String
Dim txt2 As String
Dim txt3 As String
Dim lastrow As Long
Dim r As Long
Dim Account As String

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
If Not IsNumeric(TextBox1.Text) Then
On Error GoTo ErrorHandler
Account = TextBox1.Text
Set Wkbk = Workbooks.Open("C:\Users\kameron\Desktop\Quality Improvement 
Software\Log In.xlsm")
lastrow = Wkbk.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If Wkbk.Sheets("Tables").Cells(r, 1) = Label5.Caption Then
Wkbk.Sheets("Tables").Cells(r, 1).Value = TextBox1.Text
Wkbk.Sheets("Tables").Cells(r, 2).Value = TextBox2.Text
Wkbk.Sheets("Tables").Cells(r, 3).Value = TextBox3.Text
MsgBox "Update Successful", vbInformation
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Unload EditAccount
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Enter Correct Information", vbCritical
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub

1 个答案:

答案 0 :(得分:1)

问题在于

lastrow = Wkbk.Cells(Rows.Count, "A").End(xlUp).Row

工作簿对象没有Cells属性。

在上下文中,您似乎想要

lastrow = Wkbk.Sheets("Tables").Cells(Rows.Count, "A").End(xlUp).Row

为了追踪此错误,您可以完成以下两项操作之一:

1)使用F8逐步完成代码并查看它失败的行。

2)暂时注释掉行On Error GoTo ErrorHandler并运行代码。

任何一种方法都会很快导致这条线。