In Excel using VBA code, is there a way to conditionally hide/unhide rows based on 1 cell, and then hide/unhide rows in a table based on another cell?

huangapple go评论74阅读模式
英文:

In Excel using VBA code, is there a way to conditionally hide/unhide rows based on 1 cell, and then hide/unhide rows in a table based on another cell?

问题

这是我的目前代码,用于解决我的问题的第一部分。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("D5") = "1 Term" Then
        Rows("15:166").EntireRow.Hidden = False
        Rows("167:625").EntireRow.Hidden = True
    ElseIf Range("D5") = "2 Terms" Then
        Rows("15:167").EntireRow.Hidden = True
        Rows("168:319").EntireRow.Hidden = False
        Rows("320:625").EntireRow.Hidden = True
    ElseIf Range("D5") = "3 Terms" Then
        Rows("15:320").EntireRow.Hidden = True
        Rows("321:472").EntireRow.Hidden = False
        Rows("473:625").EntireRow.Hidden = True
    ElseIf Range("D5") = "4 Terms" Then
        Rows("15:473").EntireRow.Hidden = False
        Rows("474:625").EntireRow.Hidden = False
    ElseIf Range("D5") = "None" Then
        Rows("12:625").EntireRow.Hidden = True
    End If
End Sub

当选择每个“Term”时,会出现4个表格。一旦选择了一个“Term”,我希望能够在单元格E5中输入1-150的数字,它将有条件地只显示所列的行数(在三个表格中)。

这是我的Excel表的可视化:Excel表可视化

我尝试过根据E5仅隐藏整行,但有一个表格我希望无论选择什么数字都保持可见。

以下代码没有问题!但再次提醒,有一个表格我希望无论选择什么数字都保持可见。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$5" Then
        If Target.Value = "1 Term" Then
            Rows("15:166").EntireRow.Hidden = False
            Rows("167:625").EntireRow.Hidden = True
            Range("E10:E12").EntireRow.Hidden = False ' Unhide cells E10 to E12
        ElseIf Target.Value = "2 Terms" Then
            Rows("15:167").EntireRow.Hidden = True
            Rows("168:319").EntireRow.Hidden = False
            Rows("320:625").EntireRow.Hidden = True
            Range("E10:E12").EntireRow.Hidden = True ' Hide cells E10 to E12
        ElseIf Target.Value = "3 Terms" Then
            Rows("15:320").EntireRow.Hidden = True
            Rows("321:472").EntireRow.Hidden = False
            Rows("473:625").EntireRow.Hidden = True
            Range("E10:E12").EntireRow.Hidden = True ' Hide cells E10 to E12
        ElseIf Target.Value = "4 Terms" Then
            Rows("15:473").EntireRow.Hidden = False
            Rows("474:625").EntireRow.Hidden = False
            Range("E10:E12").EntireRow.Hidden = True ' Hide cells E10 to E12
        ElseIf Target.Value = "None" Then
            Rows("12:625").EntireRow.Hidden = True
            Range("E10:E12").EntireRow.Hidden = False ' Unhide cells E10 to E12
        End If
    End If
    
    If Target.Address = "$E$5" Then
        If IsNumeric(Target.Value) Then
            If Target.Value >= 1 And Target.Value <= 150 Then
                Dim i As Integer
                For i = 16 To 166
                    If i <= Target.Value + 15 And i >= 16 Then
                        Rows(i).Hidden = False
                    Else
                        Rows(i).Hidden = True
                    End If
                Next i
                If Range("D5") = "None" Then
                    Range("A12:A14").EntireRow.Hidden = False ' Unhide cells A12 to A14
                Else
                    Range("A12:A14").EntireRow.Hidden = True ' Hide cells A12 to A14
                End If
            End If
        End If
    End If
End Sub
英文:

This is my current code that works for the first part of my solution.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range(&quot;D5&quot;) = &quot;1 Term&quot; Then
        Rows(&quot;15:166&quot;).EntireRow.Hidden = False
        Rows(&quot;167:625&quot;).EntireRow.Hidden = True
    ElseIf Range(&quot;D5&quot;) = &quot;2 Terms&quot; Then
        Rows(&quot;15:167&quot;).EntireRow.Hidden = True
        Rows(&quot;168:319&quot;).EntireRow.Hidden = False
        Rows(&quot;320:625&quot;).EntireRow.Hidden = True
    ElseIf Range(&quot;D5&quot;) = &quot;3 Terms&quot; Then
        Rows(&quot;15:320&quot;).EntireRow.Hidden = True
        Rows(&quot;321:472&quot;).EntireRow.Hidden = False
        Rows(&quot;473:625&quot;).EntireRow.Hidden = True
    ElseIf Range(&quot;D5&quot;) = &quot;4 Terms&quot; Then
        Rows(&quot;15:473&quot;).EntireRow.Hidden = False
        Rows(&quot;474:625&quot;).EntireRow.Hidden = False
    ElseIf Range(&quot;D5&quot;) = &quot;None&quot; Then
        Rows(&quot;12:625&quot;).EntireRow.Hidden = True
    End If

There are 4 tables that appear when each "Term" is selected. Once a "Term" is selected, I want to be able to put a number 1-150 in cell E5, and it will conditionally only show the number of rows (in three tables) that is listed.

Here is a visual of my Excel sheet.

I have tried just hiding full rows based on E5, but there is one table that I would like to stay visible, no matter what number is selected.

The following code worked with no issues! But again, there is one table that I want to be visible no matter what. If I put "2" in E5, then the table also disappears.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = &quot;$D$5&quot; Then
        If Target.Value = &quot;1 Term&quot; Then
            Rows(&quot;15:166&quot;).EntireRow.Hidden = False
            Rows(&quot;167:625&quot;).EntireRow.Hidden = True
            Range(&quot;E10:E12&quot;).EntireRow.Hidden = False &#39; Unhide cells E10 to E12
        ElseIf Target.Value = &quot;2 Terms&quot; Then
            Rows(&quot;15:167&quot;).EntireRow.Hidden = True
            Rows(&quot;168:319&quot;).EntireRow.Hidden = False
            Rows(&quot;320:625&quot;).EntireRow.Hidden = True
            Range(&quot;E10:E12&quot;).EntireRow.Hidden = True &#39; Hide cells E10 to E12
        ElseIf Target.Value = &quot;3 Terms&quot; Then
            Rows(&quot;15:320&quot;).EntireRow.Hidden = True
            Rows(&quot;321:472&quot;).EntireRow.Hidden = False
            Rows(&quot;473:625&quot;).EntireRow.Hidden = True
            Range(&quot;E10:E12&quot;).EntireRow.Hidden = True &#39; Hide cells E10 to E12
        ElseIf Target.Value = &quot;4 Terms&quot; Then
            Rows(&quot;15:473&quot;).EntireRow.Hidden = False
            Rows(&quot;474:625&quot;).EntireRow.Hidden = False
            Range(&quot;E10:E12&quot;).EntireRow.Hidden = True &#39; Hide cells E10 to E12
        ElseIf Target.Value = &quot;None&quot; Then
            Rows(&quot;12:625&quot;).EntireRow.Hidden = True
            Range(&quot;E10:E12&quot;).EntireRow.Hidden = False &#39; Unhide cells E10 to E12
        End If
    End If
    
    If Target.Address = &quot;$E$5&quot; Then
        If IsNumeric(Target.Value) Then
            If Target.Value &gt;= 1 And Target.Value &lt;= 150 Then
                Dim i As Integer
                For i = 16 To 166
                    If i &lt;= Target.Value + 15 And i &gt;= 16 Then
                        Rows(i).Hidden = False
                    Else
                        Rows(i).Hidden = True
                    End If
                Next i
                If Range(&quot;D5&quot;) = &quot;None&quot; Then
                    Range(&quot;A12:A14&quot;).EntireRow.Hidden = False &#39; Unhide cells A12 to A14
                Else
                    Range(&quot;A12:A14&quot;).EntireRow.Hidden = True &#39; Hide cells A12 to A14
                End If
            End If
        End If
    End If
End Sub

答案1

得分: 1

这可能是一个不错的起点:

Sub MakeVisibleNoMatterWhat()

    Dim myRow As Range
    For Each myRow In ThisWorkbook.Worksheets("Sheet1").Range("C5:F15").Rows
        Debug.Print myRow.Address
        myRow.EntireRow.Hidden = True
    Next myRow

End Sub

只需确保你正在处理的是Sheet1,或者在代码中更改工作表名称。然后,稍后尝试改进代码,将范围分配为变量并将其传递给函数。

在代码的末尾调用该函数:

End If
End If
End If
End If
MakeVisibleNoMatterWhat
End Sub
英文:

This might be a good start:

Sub MakeVisibleNoMatterWhat()

    Dim myRow As Range
    For Each myRow In ThisWorkbook.Worksheets(&quot;Sheet1&quot;).Range(&quot;C5:F15&quot;).Rows
        Debug.Print myRow.Address
        myRow.EntireRow.Hidden = True
    Next myRow

End Sub

Just make sure that you are working on Sheet1 or change the sheet name in the code. Then, later try to make the code better, assigning the range as a variable and passing it to the function.

Call the function at the end of your code:

                End If
            End If
        End If
    End If
    MakeVisibleNoMatterWhat
End Sub

huangapple
  • 本文由 发表于 2023年4月10日 22:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定