当用户在工作表上覆盖我的某个公式时,我正在尝试调用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
以下是模块I中的代码:
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
调试时,vba突出显示上面代码的最后一行:Application.Intersect((Rows(ActiveCell.Row)),Range(" AA:AA"))。选择
我确定以前曾经这样做过!我做错了吗?
提前感谢您花时间阅读本文!
蒂娜
答案 0 :(得分:0)
代码中提出了一些建议:
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 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