工作表更改事件的问题

时间:2020-01-16 03:15:31

标签: excel vba

当前问题:我有一个下拉列表,该列表通过使用静态值的数据验证进行填充。该下拉列表的范围是“ G2”。每当用户进行选择时,我都会触发一个在“ ThisWorkbook”中编码的工作表更改事件。但是,每次工作表中的任何单元格发生更改时都会触发此事件(当然,会发生)。例如,在下面的代码结尾处,我正在处理一些事情并将其写入不同的单元格范围。因此,控件进入了不确定的循环。我没主意了。如何仅将事件本地化为下拉选择?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sProgramIncrement, sSprintCycle, sSprintStart, sSprintEnd As String
sProgramIncrement = Split(Left(Range("G2"), 7), ".")(0)
sSprintCycle = Split(Left(Range("G2"), 7), ".")(1)
sSprintStart = Split(Right(Range("G2"), 7), "-")(0)
sSprintEnd = Split(Right(Range("G2"), 7), "-")(1)

Select Case Range("G2")
    Case "PI1.S01 J01-J14"
        If sProgramIncrement = "PI1" And Left(sSprintStart, 1) = "J" Then sSprintStartMonth = "01"
        If sProgramIncrement = "PI1" And Left(sSprintEnd, 1) = "J" Then sSprintEndMonth = "01"

        If Left(sSprintStart, 1) = "F" Then sSprintStartMonth = "02"
        If Left(sSprintEnd, 1) = "F" Then sSprintEndMonth = "02"

        If sProgramIncrement = "PI1" And Left(sSprintStart, 1) = "M" Then sSprintStartMonth = "03"

        If sProgramIncrement = "PI2" And sSprintCycle = "S01" And Left(sSprintStart, 1) = "M" Then sSprintStartMonth = "03"

        If sProgramIncrement = "PI2" And Left(sSprintStart, 1) = "A" Then sSprintStartMonth = "04"

        If Left(sSprintStart, 1) = "M" Then sSprintStartMonth = "05"

        If sProgramIncrement = "PI2" And Left(sSprintStart, 1) = "J" Then sSprintStartMonth = "06"

        If sProgramIncrement = "PI3" And Left(sSprintStart, 1) = "J" Then sSprintStartMonth = "07"

        If sProgramIncrement = "PI3" And Left(sSprintStart, 1) = "A" Then sSprintStartMonth = "08"

        If Left(sSprintStart, 1) = "S" Then sSprintStartMonth = "09"
        If Left(sSprintEnd, 1) = "S" Then sSprintEndMonth = "09"

        If Left(sSprintStart, 1) = "O" Then sSprintStartMonth = "10"
        If Left(sSprintEnd, 1) = "O" Then sSprintEndMonth = "10"

        If Left(sSprintStart, 1) = "N" Then sSprintStartMonth = "11"
        If Left(sSprintEnd, 1) = "N" Then sSprintEndMonth = "11"

        If Left(sSprintStart, 1) = "D" Then sSprintStartMonth = "12"
        If Left(sSprintEnd, 1) = "D" Then sSprintEndMonth = "12"

        Worksheets("KPIs").Range("G3") = sSprintStartMonth & "/" & Right(sSprintStart, 2) & "/" & "20"
        Worksheets("KPIs").Range("I3") = sSprintStartMonth & "/" & Right(sSprintEnd, 2) & "/" & "20"


End Select
End Sub

enter image description here

0 个答案:

没有答案
相关问题