【问题标题】:Worksheet Change: Multiple target values not working工作表更改:多个目标值不起作用
【发布时间】:2019-02-15 06:09:46
【问题描述】:

需要根据另一个工作表的单元格中的下拉单元格值隐藏/取消隐藏多个 Excel 工作表中的行。 我有 3 张工作表,仪表板,数据输入,指标表, 我已将以下代码放入仪表板表中。下面提到的 3 个单元格是下拉列表,包含不同的值集。

当用户在下拉列表 C4 中选择一个值时,我将隐藏/取消隐藏上述所有 3 张工作表中的某些行。但只有单元格 C4 的选择案例可以正常工作。如您所见,我也有 C23 和 C32 的公式,但它似乎不起作用,不确定是由于在同一张纸上的操作

另一个例子,当我为单元格 C23 选择一个下拉值时。下面 C23 下列出的操作可以正常工作(调用相应的宏并且隐藏/取消隐藏行)。但是当我检查之前对单元格值 C4 的选择时,它就消失了。

请帮忙

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("C4,C23,C32")) Is Nothing Then  

For Each c In Intersect(Target, Range("C4,C23,C32"))
    Select Case c.Address(0, 0)

        Case "C4"
            If Target.Value = "Ecommerce" Then Call Ecommerce
            If Target.Value = "Non-Commerce" Then Call NonCommerce
            If Target.Value = "Ecommerce & Non-Commerce" Then Call Both
            If Target.Value = "Select Ecommerce/Non-Commerce" Then Call Both

        Case "C23"
            If Target.Value = "Select Year" Then Call SelectYear
            If Target.Value = "2020" Then Call Twentytwenty
            If Target.Value = "2021" Then Call TwentyOne
            If Target.Value = "2022" Then Call TwentyTwo
            If Target.Value = "2023" Then Call TwentyThree
            If Target.Value = "2024" Then Call TwentyFour
            If Target.Value = "2025" Then Call TwentyFive

        Case "C32"
            If Target.Value = "Select PPC" Then Call SelectPPC
            If Target.Value = "PPC 2" Then Call PPCTwo
            If Target.Value = "PPC 3" Then Call PPCThree
            If Target.Value = "PPC 4" Then Call PPCFour
            If Target.Value = "PPC 5" Then Call PPCFive
            If Target.Value = "PPC 6" Then Call PPCSix
            If Target.Value = "PPC 7" Then Call PPCSeven

        End Select
Next c
End If
End Sub

我希望工作表同时对所有 3 个单元格进行操作,现在它已损坏,一次只能工作一个。感谢您的帮助。

【问题讨论】:

  • 这与您的问题无关,所以我很抱歉,但是看到您每年都有一个单独的宏,您确定有必要吗?您是否学会了如何在宏中使用参数?这样,您几乎可以完全删除 If 语句,只需调用 Case "C23" // Call myYearMacro(Target.Value)?
  • 你想要c.Value 而不是Target.Value
  • @BruceWayne - 是的,我会这样做来优化代码。感谢您的回复。
  • @ScottCraner - 我改变了,但没有帮助。
  • 一个快速测试 - 而不是 Select Case 的三个选项,如果你只是做了 If c.Address(0,0) = "C4" Then // [those options] // End If // If c.Address(0,0) = "C23" Then // [those options] // End If // If c.Address(0,0) = "C32" Then // [those options // End If。这样,它会检查c.Address() 三次,而不是一次...?

标签: excel vba


【解决方案1】:

阅读 cmets,听起来好像每次您从三个下拉菜单之一中选择不同的值时,您都想运行三个宏,具体取决于所选的值。如果是这种情况,那么您不需要遍历目标单元格(只能分配一个,无论如何使用下拉菜单)。

您需要做的就是确定三个单元格之一已更改,然后根据三个特定单元格运行相应的宏。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' This is all that you have to check, before deciding to run the macros
    If Intersect(Target, Range("C4,C23,C32")) Is Nothing Then Exit Sub

    Select Case Range("C4").Value
    Case "Ecommerce"
        Call Ecommerce
    Case "Non-Commerce"
        Call NonCommerce
    Case "Ecommerce & Non-Commerce", "Select Ecommerce/Non-Commerce"
        Both
    End Select

    Select Case Range("C23").Value
    Case "Select Year"
        Call SelectYear
    Case "2020"
        Call Twentytwenty
    Case "2021"
        Call TwentyOne
    Case "2022"
        Call TwentyTwo
    Case "2023"
        Call TwentyThree
    Case "2024"
        Call TwentyFour
    Case "2025"
        Call TwentyFive
    End Select

    Select Case Range("C32").Value
    Case "Select PPC"
        Call SelectPPC
    Case "PPC 2"
        Call PPCTwo
    Case "PPC 3"
        Call PPCThree
    Case "PPC 4"
        Call PPCFour
    Case "PPC 5"
        Call PPCFive
    Case "PPC 6"
        Call PPCSix
    Case "PPC 7"
        Call PPCSeven
    End Select
End Sub

或者更好的是,听 BruceWayne,你最终会得到这样的结果:

Private Sub Worksheet_Change(ByVal Target As Range)
    ' This is all that you have to check, before deciding to run the macros
    If Intersect(Target, Range("C4,C23,C32")) Is Nothing Then Exit Sub

    Select Case Range("C4").Value
    Case "Ecommerce"
        Ecommerce
    Case "Non-Commerce"
        NonCommerce
    Case "Ecommerce & Non-Commerce", "Select Ecommerce/Non-Commerce"
        Both
    End Select

    If IsNumeric(Range("C23").Value) Then SelectYear CInt(Range("C23").Value)

    If IsNumeric(Mid(Range("C32").Value, 5)) Then SelectPPC CInt(Mid(Range("C32").Value, 5))
End Sub

Sub SelectYear(Year As Integer)
    ' Do stuff
End Sub

Sub SelectPPC(Value As Integer)
    ' Do stuff
End Sub

仅供参考,您实际上不需要使用Call。当Sub-routine 有参数时,它唯一真正的区别是是否需要括号。

【讨论】:

  • +1 仅供参考。我不记得何时使用或不使用括号作为参数,但这很简单!
  • 谢谢@BruceWayne。您的建议有所帮助。我的模块宏也有一点问题。更正并使用您的建议后,它工作正常。感谢您的帮助
猜你喜欢
  • 2021-02-10
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多