如何在整个工作表中使用(ByVal Target as Range)多个范围 - VBA?
How to use (ByVal Target as Range) for multiple ranges throughout sheets - VBA?
所以我试图创建一个代码来触发基于多个范围的事件的宏,范围因 sheet 而异所以我计划添加具有不同范围的事件代码每个的 Sheet 对象选项卡。当涉及两个不同的范围时,我的代码工作得很好。但是当我添加第三个范围时,出现以下错误:
下面附上我的模块代码和我在每个 sheet.
上放置的(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 的初学者,所以我有点迷茫,非常感谢任何帮助。
提前致谢!
IIUC,您正在寻找如下参考范围:
Set rngToCheck = Intersect(Target, Me.Range("E5:E36,P5:P36,Q5:Q36"))
来自 Range
文档:
Use Range (cell1, cell2), where cell1 and cell2 are Range objects that specify the start and end cells, to return a Range object
您没有指定开始和结束单元格,而是指定了相关范围的不同区域。所以你希望范围地址都在引号内。
尝试使用
Intersect(Target, Union(Me.Range("E5:E36"), Me.Range("P5:P36")))
而不是
Intersect(Target, Me.range("E5:E36", "P5:P36"))
.
所以我试图创建一个代码来触发基于多个范围的事件的宏,范围因 sheet 而异所以我计划添加具有不同范围的事件代码每个的 Sheet 对象选项卡。当涉及两个不同的范围时,我的代码工作得很好。但是当我添加第三个范围时,出现以下错误:
下面附上我的模块代码和我在每个 sheet.
上放置的(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 的初学者,所以我有点迷茫,非常感谢任何帮助。
提前致谢!
IIUC,您正在寻找如下参考范围:
Set rngToCheck = Intersect(Target, Me.Range("E5:E36,P5:P36,Q5:Q36"))
来自 Range
文档:
Use Range (cell1, cell2), where cell1 and cell2 are Range objects that specify the start and end cells, to return a Range object
您没有指定开始和结束单元格,而是指定了相关范围的不同区域。所以你希望范围地址都在引号内。
尝试使用
Intersect(Target, Union(Me.Range("E5:E36"), Me.Range("P5:P36")))
而不是
Intersect(Target, Me.range("E5:E36", "P5:P36"))
.