我有一个数字从0到10的下拉列表。零隐藏第1到10行,1仅显示第1行,2仅显示第1和2行,依此类推,直到nbr10。这按预期工作,我可以从nbr 1上升到10,并显示所有正确的行数。
例如,如果我选择了第5行,然后选择了第2行,那么我将无法隐藏第5到3行。
我对VBA还是很陌生,如果我添加另一行将相同的nbr设置为EntireRow.Hidden = true,则出于明显的原因而无法正常工作,因为相同的数字冲突的动作。有人可以帮我吗?
If Target.Address = Range("SRUAdd").Address Then
If Range("SRUAdd").Value = "0" Then ' hide rows 1 to 10
Rows((Target.Row + 4) & ":" & (Target.Row + 13)).EntireRow.Hidden = True
ElseIf Range("SRUAdd").Value = "1" Then ' show row #1
Rows(Target.Row + 4).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "2" Then ' show row #2
Rows((Target.Row + 4) & ":" & (Target.Row + 5)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "3" Then ' show row #3
Rows((Target.Row + 4) & ":" & (Target.Row + 6)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "4" Then ' show row #4
Rows((Target.Row + 4) & ":" & (Target.Row + 7)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "5" Then ' show row #5
Rows((Target.Row + 4) & ":" & (Target.Row + 8)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "6" Then ' show row #6
Rows((Target.Row + 4) & ":" & (Target.Row + 9)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "7" Then ' show row #7
Rows((Target.Row + 4) & ":" & (Target.Row + 10)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "8" Then ' show row #8
Rows((Target.Row + 4) & ":" & (Target.Row + 11)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "9" Then ' show row #9
Rows((Target.Row + 4) & ":" & (Target.Row + 12)).EntireRow.Hidden = False
ElseIf Range("SRUAdd").Value = "10" Then ' show row #10
Rows((Target.Row + 4) & ":" & (Target.Row + 13)).EntireRow.Hidden = False
End If
End If
期望的结果是我想根据从下拉列表中选择的数字显示和隐藏正确的行。当前结果是仅基于下拉选择显示正确的行。
答案 0 :(得分:0)
逻辑是隐藏所有内容,然后取消隐藏相关行。实际上,您不需要条件If Range("SRUAdd").Value = "0" Then
。直接从If Range("SRUAdd").Value = "1" Then
开始。您也可以使用Select Case
。
这是您要尝试的(未经测试)吗?
Dim sRow As Long, toRow As Long
If Target.Address = Range("SRUAdd").Address Then
Rows((Target.Row + 4) & ":" & (Target.Row + 13)).EntireRow.Hidden = True
sRow = Target.Row + 2
Select Case Range("SRUAdd").Value
Case 1: toRow = Target.Row + 4
Case 2: toRow = Target.Row + 5
Case 3: toRow = Target.Row + 6
Case 4: toRow = Target.Row + 7
Case 5: toRow = Target.Row + 8
Case 6: toRow = Target.Row + 9
Case 7: toRow = Target.Row + 10
Case 8: toRow = Target.Row + 11
Case 9: toRow = Target.Row + 12
Case 10: toRow = Target.Row + 13
End Select
If toRow <> 0 Then Rows(sRow & ":" & toRow).EntireRow.Hidden = False
End If
编辑:
我可以用Rows((Target.Row + 44)&“:”&(Target.Row + 53))。EntireRow.Hidden = True代码隐藏第二张表,我不知道如何添加案例部分的额外代码。我已经尝试了一些方法,但是它不起作用...我认为“案例1,案例2”等与在下拉框中选择的数字有关?我已经尝试过案例1:toRow = Target.Row + 4 + 44,但这是行不通的。 – stacson76 3小时前
您必须单独处理表格。看到这个。
Dim sRowTblA As Long, toRowTblA As Long
Dim sRowTblB As Long, toRowTblB As Long
If Target.Address = Range("SRUAdd").Address Then
Rows((Target.Row + 4) & ":" & (Target.Row + 13)).EntireRow.Hidden = True
Rows((Target.Row + 44) & ":" & (Target.Row + 53)).EntireRow.Hidden = True
sRowTblA = Target.Row + 2
sRowTblB = Target.Row + 44
Select Case Range("SRUAdd").Value
Case 1
toRowTblA = Target.Row + 4
toRowTblB = Target.Row + 44
Case 2
toRowTblA = Target.Row + 5
toRowTblB = Target.Row + 45
Case 3
toRowTblA = Target.Row + 6
toRowTblB = Target.Row + 46
Case 4
toRowTblA = Target.Row + 7
toRowTblB = Target.Row + 47
'
'~~> And So on
'
End Select
If toRowTblA <> 0 Then Rows(sRowTblA & ":" & toRowTblA).EntireRow.Hidden = False
If toRowTblB <> 0 Then Rows(sRowTblB & ":" & toRowTblB).EntireRow.Hidden = False
End If