2016-10-19 4 views
0

셀 값 (1-10 사이)을 기준으로 행을 숨기거나 표시하는 섹션이있는 시트 작업 중입니다. 지금은 몇 가지 중첩 if 문이 있습니다. 이로 인해 내 통합 문서가 고통스럽게 느려졌습니다. 이 코드를 축소하는 방법이 있습니까? 감사.셀 값을 기반으로 행을 숨기는 VBA 매크로

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 
+2

Select Case를 사용하거나 옵션을 ElseIF로 조정하면 하단의 추가 End IF가 모두 손실 될 수 있습니다. – Rdster

+0

감사. 코드가 작동하므로 codereview 섹션에서도 알아볼 것입니다. 워크 북의 속도를 높이 려구요. – zanzibar

답변

1

. 나는 한 번 살펴 보았고 더 수학적으로 만들려고 시도했다. 코드가 짧아졌다. 이 작동하는 경우를 참조하십시오 : Rows 또는 '에는 EntireColumn when using Columns`를 사용하는 경우

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 
+0

예 - 감사합니다! 이것은 잘 작동하며 통합 문서 전반에 걸쳐 구현됩니다. – zanzibar

+0

적용하고자하는 곳이 여러 곳인 경우이 Sub를 워크 시트 변경으로 호출하는 방법은 무엇입니까? 감사. – zanzibar

+0

@ 잔지바르 왜 워크 시트 변경 이벤트가 발생합니까? 여러 범위에서 실행하려는 경우 하위 범위로 먼저 전달할 수 있습니다. – BruceWayne

1

케이스 문을 사용해 볼 수도 있습니다.

아, 아니면 최소한 EndIf 문을 줄이는 ElseIf 옵션을 사용하십시오. .

가 나는 경우 코드는 다음과 같이 생겼다고 생각 :

선택 범위 ("B87") 값을

Case "1" 

Case "2" 

... 

종료를 선택 당신은 기본적으로 동일한 코드의 전체 많이

1

당신은 EntireRow를 사용할 필요가 없습니다.

Rows("88:98").Hidden = True 

If Range("B87").Value > 0 Then 
    Rows(88).Resize(1 + Range("B87").Value).Hidden = False 
End If