Excel 如何将(ByVal目标作为范围)用于整个图纸的多个范围-VBA?
因此,我尝试创建一个代码,该代码基于多个范围的事件触发宏,范围因工作表而异,因此我计划添加事件代码,在每个工作表对象选项卡中具有不同的范围。当代码涉及两个不同的范围时,我的工作非常出色。但是,当我添加第三个范围时,会出现以下错误: 下面我将附上我的模块代码和我放在每张纸上的(ByVal目标范围)代码Excel 如何将(ByVal目标作为范围)用于整个图纸的多个范围-VBA?,excel,vba,event-driven,Excel,Vba,Event Driven,因此,我尝试创建一个代码,该代码基于多个范围的事件触发宏,范围因工作表而异,因此我计划添加事件代码,在每个工作表对象选项卡中具有不同的范围。当代码涉及两个不同的范围时,我的工作非常出色。但是,当我添加第三个范围时,会出现以下错误: 下面我将附上我的模块代码和我放在每张纸上的(ByVal目标范围)代码 Private Sub Worksheet_Change(ByVal Target As range) Dim rngToCheck As range Se
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的初学者,所以我有点不知所措,任何帮助都将不胜感激
提前谢谢你 IIUC,您正在寻找如下参考范围:
Set rngToCheck = Intersect(Target, Me.Range("E5:E36,P5:P36,Q5:Q36"))
从文件中:
使用Range(cell1、cell2),其中cell1和cell2是指定起始单元格和结束单元格的Range对象,以返回范围对象
您没有指定起始单元格和结束单元格,而是指定所讨论范围的不同区域。因此,您希望范围地址全部包含在引号内。尝试使用
Intersect(目标、联合(Me.Range(“E5:E36”)、Me.Range(“P5:P36”))
而不是
Intersect(Target,Me.range(“E5:E36”,“P5:P36”))
哪一行抛出错误?你的意思是说你尝试了类似于Set rngToCheck=Intersect(Target,Me.range(“E5:E36”、“P5:P36”、“Q5:Q36”)
?确切地说,它突出显示了Me.range部分。在Final\u Action\u所采取的过程调用之后的任何操作都将抛出错误,因为这些过程在此代码段中没有标识。另外,您如何在所采取的最终行动
过程中定义ActiveCell
?什么是Q5:36
?