首页 > 解决方案 > 下拉自定义隐藏和取消隐藏

问题描述

我的期望:

我希望代码以这样一种方式运行,即当我选择 PM 时,它应该只显示 PM。当我选择 PM 和 SCM 时,它们都应该被看到,而其他部门应该被隐藏。

当我为该单元格选择NONE时,该特定部门应该被隐藏,而其他部门(如果有的话)应该仍然可见。

在此处输入图像描述

在此处输入图像描述

标签: excelvba

解决方案


我的查询代码


Sub Hide2ndFix()
'
' Hide2ndFix Macro
'
Dim RowCnt As Long, uRng As Range
Dim str1 As String
Dim str2 As String
Dim str3 As String

Dim s As Range

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

BeginRow = 2
EndRow = 20
ChkCol = 1
Set s = Range("A2:A20")

With s
.EntireRow.Hidden = True
End With

str1 = Range("C3").Value

str2 = Range("C4").Value

str3 = Range("C5").Value

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = str1 Or Cells(RowCnt, ChkCol).Value = str2 Or Cells(RowCnt, ChkCol).Value = str3 Then
         If uRng Is Nothing Then
          Set uRng = Cells(RowCnt, ChkCol)

         Else
          Set uRng = Union(uRng, Cells(RowCnt, ChkCol))

         End If

        End If
    Next RowCnt

 If Not uRng Is Nothing Then uRng.EntireRow.Hidden = False


Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub

推荐阅读