英文:
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("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
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 = "$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
答案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("Sheet1").Range("C5:F15").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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论