2015-12-09 81 views
2

我對VBa非常陌生,並且正在嘗試通過構建或複製現有的vba工作表進行學習。 在這其中,我在下面的代碼得到一個錯誤:錯誤424對象需要 - 似乎找不到錯誤

Private Sub lstLookup_DblClick(ByVal Cancel As MSForms.ReturnBoolean) 
'declare the variables 
Dim cPayroll As String 
Dim I As Integer 
Dim findvalue 
'error block 
On Error GoTo errHandler: 
'get the select value from the listbox 
For I = 0 To lstLookup.ListCount - 1 
    If lstLookup.Selected(I) = True Then 
     cPayroll = lstLookup.List(I, 1) 
    End If 
Next I 
'find the payroll number 
Set findvalue = Sheet2.Range("F:F").Find(What:=cPayroll, LookIn:=xlValues).Offset(0, -3) 
'add the database values to the userform 
cNum = 21 
For X = 1 To cNum 
    Me.Controls("Reg" & X).Value = findvalue 
    Set findvalue = findvalue.Offset(0, 1) 
Next 
'disable adding 
Me.cmdAdd.Enabled = False 
Me.cmdEdit.Enabled = True 
'error block 
On Error GoTo 0 
Exit Sub 
errHandler:: 
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _ 
     & Err.Number & vbCrLf & Err.Description & vbCrLf & _ 
     "Please notify the administrator" 
End Sub 

這是給我的錯誤:「所需的424對象」

我似乎無法找出錯誤

燦誰來幫幫我?

在此先感謝。

+2

一件事是'昏暗findvalue作爲Range'。不要留空,因爲它將其設置爲Variant,這可能是也可能不是你想要的。否則,你的代碼錯誤在哪一行? –

+0

感謝您的快速回復,錯誤發生在: '禁止添加 Me.cmdAdd.Enabled = False Me.cmdEdit.Enabled = True –

回答

1

變化

Me.cmdAdd.Enabled = False 
Me.cmdEdit.Enabled = True 

Me.Controls("cmdAdd").Enabled = False 
Me.Controls("cmdEdit").Enabled = True