需要基于另一个工作表单元格中的下拉单元格值来隐藏/取消隐藏多个Excel工作表中的行。 我有3张纸,Dashbaord,数据输入,指标表, 我已经将以下代码放在仪表板工作表中。下面提到的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个单元,但现在它已经坏了,一次只能工作一次。感谢您的帮助。
答案 0 :(得分:1)
在读取注释时,听起来好像每次您从三个下拉菜单之一中选择一个不同的值时,您都想运行三个宏,具体取决于所选的值。如果是这种情况,那么您就不需要遍历目标单元格(无论如何,都可以使用下拉菜单分配一个单元格)。
您需要做的就是确定三个单元格之一已更改,然后根据三个特定的单元格运行相应的宏。
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
例程具有参数时,是否需要括号。