尝试触发代码时所需的运行时错误424对象
我目前正试图调用一个VBA模块,当用户覆盖我的公式之一,在工作表上。 我认为工作表更改事件正在触发,但是当执行模块时,我得到了运行时错误424(“Object Required”)。 我不确定我做错了什么?
这是我的工作表更改事件代码:
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("Award_Amount")) Is Nothing Then Call Award_Amount_Entered End If If Not Intersect(Target, Range("Award_two_Amount")) Is Nothing Then Call Award_two_Amount_Entered End If End Sub
以下是我要调用的模块中的代码:
Sub Award_Amount_Entered() 'If the user has overwritten the formula that was just in the cell If ActiveCell.HasFormula = False Then Applicaton.Intersect((Rows(ActiveCell.Row)), Range("AA:AA")).Select ....run some more code End If End Sub
debugging时,vba突出显示上面代码的最后一行:Application.Intersect((Rows(ActiveCell.Row)),Range(“AA:AA”))。Select
我敢肯定,这曾经工作过! 难道我做错了什么?
在此先感谢您花时间阅读本文!
蒂娜
代码中提出了一些build议:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Set rng = Intersect(Target, Me.Range("Award_Amount")) If Not rng Is Nothing Then Award_Amount_Entered rng '<<< pass rng to the called procedure End If Set rng = Intersect(Target, Me.Range("Award_two_Amount")) If Not rng Is Nothing Then Award_two_Amount_Entered rng '<<< pass rng to the called procedure End If End Sub
被称为Sub:
Sub Award_Amount_Entered(rng As Range) Dim c As Range, c2 As Range 'Remember, Target can be a multi-cell range, so loop over ' all of the cells in the "rng" parameter... On Error GoTo haveError 'handle any errors For Each c In rng.Cells 'If the user has overwritten the formula that was just in the cell If Not c.HasFormula Then 'Use a range variable instead of selecting the cell ' and then operating on the selection Set c2 = c.EntireRow.Cells(1, "AA") 'If you're going to update the sheet here then it's good practice ' to disable events Application.EnableEvents = False 'update sheet Application.EnableEvents = True End If End If Exit Sub haveError: 'If your code errors you need to make sure you re-enable events ' (at the very least) before exiting Application.EnableEvents = True End Sub