【发布时间】:2020-05-28 04:14:12
【问题描述】:
所以我正在尝试创建一个代码,该代码基于多个范围的事件触发宏,范围因工作表而异,因此我计划在每个工作表对象选项卡中添加具有不同范围的事件代码.当涉及两个不同的范围时,我的代码工作得很好。但是当我添加第三个范围时,它给了我以下错误:
下面附上我的模块代码和我放在每张纸上的(ByVal Target as Range)代码。
Private Sub Worksheet_Change(ByVal Target As range)
Dim rngToCheck As range
Set rngToCheck = Intersect(Target, Me.range("E5:E36", "P5:P36"))
If rngToCheck Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
Dim rng As range
For Each rng In rngToCheck
Select Case rng.Value
Case "Final Action Taken"
Final_Action_Taken
Case "Populate Non Final Action Taken Date"
EnterNonFinal_Date
Case "Populate Previous SPD Submission"
SPD_PreviousSubmission
Case "Final Action Taken SPD"
Final_Action_TakenSPD
End Select
Next
SafeExit:
Application.EnableEvents = True
End Sub
Sub Final_Action_Taken()
'Ask for the date of last submission of document by business
If ActiveCell = "Final Action Taken" Then
Dim myValue As Variant, Output As range
myValue = InputBox("Please enter the final document submission date for the current Sign Off Year.", "Final Submission Date")
Set Output = ActiveCell.Offset(0, 2)
Output = myValue
End If
End Sub
Sub EnterNonFinal_Date()
If ActiveCell = "Populate Non Final Action Taken Date" Then
Dim Output As range
Dim myValue As Variant
myValue = "=today()"
Set Output = ActiveCell.Offset(0, 2)
Output = myValue
End If
End Sub
Sub SPD_PreviousSubmission()
'Ask for the date of last submission of document by business
If ActiveCell = "Populate Previous SPD Submission" Then
Dim myValue As Variant
Dim Output As range
Dim Output2 As range
Dim myValue2 As Variant
myValue = InputBox("Please enter the Previous SPD Submission Date.", "Previous SPD Submission Date")
Set Output = ActiveCell.Offset(0, 1)
Output = myValue
myValue2 = "=today()"
Set Output2 = ActiveCell.Offset(0, 2)
Output2 = myValue2
End If
End Sub
Sub Final_Action_TakenSPD()
'Ask for the date of last submission of document by business
If ActiveCell = "Final Action Taken SPD" Then
Dim myValue As Variant, Output As range
myValue = InputBox("Please enter the final document submission date for the current Sign Off Year.", "Final Submission Date")
Set Output = ActiveCell.Offset(0, 2)
Output = myValue
End If
End Sub
请注意,我是 VBA 的初学者,所以我有点迷茫,任何帮助将不胜感激。
提前谢谢你!
【问题讨论】:
-
哪一行会报错?你的意思是你尝试过
Set rngToCheck = Intersect(Target, Me.range("E5:E36", "P5:P36", "Q5:Q36"))之类的东西吗? -
正是如此^,它突出显示了 me.Range 部分。
-
Final_Action_Taken的过程调用之后的任何内容都会引发错误,因为这些过程在此代码 sn-p 中未标识。另外,您如何在Final_Action_Taken过程中定义ActiveCell?
标签: excel vba event-driven