我有一个数据表,其中,需要根据在不同单元格集中设置的标准隐藏行集。我写了一些基本上是“洞穴人编码”的东西,当然它不起作用:)
我创建了if并尝试将ifs放在ifs中,但实际上这个代码没有发生任何事情。
我只写了2组行,但它是大约30个不同的集合(尚未编写)
Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range(Cells(7, 8), Cells(7, 8))
Set Target1 = Range(Cells(3, 2), Cells(3, 2))
If Target.Value = "No"
Rows("8:29").EntireRow.Hidden = True
If Target.Value = "Yes" And Target1.Value = "Half" Then
Rows("22:29").EntireRow.Hidden = True
ElseIf Target.Value = "Yes" And Target1.Value = "Full" Then
Rows("8:29").EntireRow.Hidden = False
End If
End If
Set Target = Range(Cells(30, 8), Cells(30, 8))
Set Target1 = Range(Cells(3, 2), Cells(3, 2))
If Target.Value = "No"
Rows("31:56").EntireRow.Hidden = True
If Target.Value = "Yes" And Target1.Value = "Half" Then
Rows("47:56").EntireRow.Hidden = True
ElseIf Target.Value = "Yes" And Target1.Value = "Full" Then
Rows("31:56").EntireRow.Hidden = False
End If
End If
End Sub
所以,在一个简短的总结中,我在单元格B3中有两个变量(Half和Full)
在指定单元格中的行H(例如H7,H30等)是/否选项。第7行,第30行(任何行,具有是/否选项)是主题的标题,包括本主题的8到29个细节。
如果H7(H30 ......)为否 - 应隐藏整个细节(第8-29行; 31-56等等) - B3中的Valua无关紧要。如果H7(H30 ...)为是,则B3中的值很重要:如果H7(H30 ...)为是并且B3是半行22-29隐藏(47-56等等),则行8-21是在这种情况下取消隐藏如果H7(H30 ...)为是并且B3已满 - 则取消隐藏行8-29。
希望我解释得很好。
请帮我改进我的代码,以便能够设定目标。
你缺少if语句,也缺少ifif
以下代码应该适合您:
Sub Worksheet_Change(ByVal Target As Range)
Set Target1 = Range(Cells(3, 2), Cells(3, 2))
' ------------
Set Target = Range(Cells(7, 8), Cells(7, 8))
If Target.Value = "No" Then
Rows("8:29").EntireRow.Hidden = True
ElseIf Target.Value = "Yes" And Target1.Value = "Half" Then
Rows("22:29").EntireRow.Hidden = True
ElseIf Target.Value = "Yes" And Target1.Value = "Full" Then
Rows("8:29").EntireRow.Hidden = False
End If
' ------------
Set Target = Range(Cells(30, 8), Cells(30, 8))
If Target.Value = "No" Then
Rows("31:56").EntireRow.Hidden = True
ElseIf Target.Value = "Yes" And Target1.Value = "Half" Then
Rows("47:56").EntireRow.Hidden = True
ElseIf Target.Value = "Yes" And Target1.Value = "Full" Then
Rows("31:56").EntireRow.Hidden = False
End If
End Sub
由于Target1范围参考不会更改,因此您无需在整个代码中重复设置它。