【发布时间】: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()三次,而不是一次...?