【问题标题】:How to use (ByVal Target as Range) for multiple ranges throughout sheets - VBA?如何在整个工作表中使用(ByVal Target as Range)多个范围 - VBA?
【发布时间】: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


【解决方案1】:

IIUC,您正在寻找如下参考范围:

Set rngToCheck = Intersect(Target, Me.Range("E5:E36,P5:P36,Q5:Q36"))

来自Range 文档:

使用范围cell1cell2),其中cell1cell2Range 对象,指定开始和结束单元格,返回 Range 对象

您没有指定开始和结束单元格,而是指定相关范围的不同区域。所以你希望范围地址都在引号内。

【讨论】:

  • 什么是Q5:36
【解决方案2】:

尝试使用

Intersect(Target, Union(Me.Range("E5:E36"), Me.Range("P5:P36")))

而不是

Intersect(Target, Me.range("E5:E36", "P5:P36")).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2012-06-13
    • 2021-09-25
    • 1970-01-01
    • 2019-04-04
    • 2021-08-15
    • 2018-10-18
    相关资源
    最近更新 更多