[英]VBA macro for hiding rows based on cell value
I am working on a sheet that has sections which hides/shows a number of rows based on a cell value (between 1-10). 我正在工作的工作表具有根据单元格值(在1至10之间)隐藏/显示许多行的部分。 At the moment, I have a handful of nested if statements.
目前,我有一些嵌套的if语句。 This has made my workbook painfully slow.
这使我的工作簿非常缓慢。 Is there a way to shrink this code?
有没有办法缩小此代码? Thanks.
谢谢。
If Range("B87").Value = 10 Then
Rows("88:98").EntireRow.Hidden = False
Else
If Range("B87").Value = 9 Then
Rows("98").EntireRow.Hidden = True
Rows("88:97").EntireRow.Hidden = False
Else
If Range("B87").Value = 8 Then
Rows("97:98").EntireRow.Hidden = True
Rows("88:96").EntireRow.Hidden = False
Else
If Range("B87").Value = 7 Then
Rows("96:98").EntireRow.Hidden = True
Rows("88:95").EntireRow.Hidden = False
Else
If Range("B87").Value = 6 Then
Rows("95:98").EntireRow.Hidden = True
Rows("88:94").EntireRow.Hidden = False
Else
If Range("B87").Value = 5 Then
Rows("94:98").EntireRow.Hidden = True
Rows("88:93").EntireRow.Hidden = False
Else
If Range("B87").Value = 4 Then
Rows("93:98").EntireRow.Hidden = True
Rows("88:92").EntireRow.Hidden = False
Else
If Range("B87").Value = 3 Then
Rows("92:98").EntireRow.Hidden = True
Rows("88:91").EntireRow.Hidden = False
Else
If Range("B87").Value = 2 Then
Rows("91:98").EntireRow.Hidden = True
Rows("88:90").EntireRow.Hidden = False
Else
If Range("B87").Value = 1 Then
Rows("90:98").EntireRow.Hidden = True
Rows("88:89").EntireRow.Hidden = False
Else
If Range("B87").Value = 0 Then
Rows("88:98").EntireRow.Hidden = True
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
I might try a case statement. 我可能会尝试案例说明。
Oh, or even use the ElseIf option which would reduce the amount of EndIf statements at the very least. 哦,甚至使用ElseIf选项,这将至少减少EndIf语句的数量。
I think the case code looks something like this: 我认为案例代码看起来像这样:
Select Range("B87").value 选择范围(“ B87”)。value
Case "1"
Case "2"
...
End Select 结束选择
You have a whole lot of basically the same code. 您有很多基本相同的代码。 I took a look and tried to make it more arithmetical, which shortens the code.
我看了一下,试图使其更具算术性,从而缩短了代码。 See if this works:
查看是否可行:
Sub t()
Dim myVal As String
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
mainRow = 98
myVal = Range("B87").Value
If myVal = 10 Then
Rows(mainRow - 10 & ":" & mainRow - 10 + myVal).EntireRow.Hidden = False
ElseIf myVal >= 1 And myVal <= 9 Then
tweakRow = mainRow - 10
row1 = (mainRow - (9 - myVal))
row2 = (mainRow - (10 - myVal))
Set hideRange = Rows(row1 & ":" & mainRow).EntireRow
Set showRange = Rows(tweakRow & ":" & row2).EntireRow
Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
hideRange.Hidden = True
showRange.Hidden = False
ElseIf myVal = 0 Then
Rows(mainRow - 10 & ":" & mainRow).EntireRow.Hidden = True
End If
End Sub
You don't need to use EntireRow
when using Rows
or 'EntireColumn when using
Columns`. 你并不需要使用
EntireRow
使用时Rows
或“EntireColumn when using
Columns`。
Rows("88:98").Hidden = True
If Range("B87").Value > 0 Then
Rows(88).Resize(1 + Range("B87").Value).Hidden = False
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.