2016-07-27 9 views
2

2013 그래서 나는 여러 가지 조건을 기반으로 VBA를 사용하여 엑셀 2013에서 행을 숨길 시도하고있다 :숨기기 행

  • 섹션의 제목이 "미사용"숨기기 섹션을 참조하십시오. 각 섹션은이 작업을보다 쉽게하기 위해 명명 된 범위입니다.
  • 행이 "Cblank"라는 이름의 범위의 일부인 경우 숨 깁니다.
  • 이제 어려운 부분에 대해 - C.Value = ""및 C.Columns (41) .Value = ""이면 범위의 각 셀 ("CNonTest")에 대해 숨긴 다음 숨 깁니다.

범위 ("CNonTest")는 Col C로 표시됩니다. 확인해야하는 추가 열은 Col AQ입니다.

난이도가 높아질 때마다이 매크로는 8 가지 유효성 검사 상자 중 하나가 변경 될 때마다 실행해야합니다. 이 코드가 시트에 다음

Sub CompHide() 

    With Sheets("Comparison").Cells 
     .EntireRow.Hidden = False 

    If Range("C9").Value = "Unused" Then 
     Range("CMarket1").EntireRow.Hidden = True 
    End If 

    If Range("C115").Value = "Unused" Then 
     Range("CMarket2").EntireRow.Hidden = True 
    End If 

    If Range("C221").Value = "Unused" Then 
     Range("CMarket3").EntireRow.Hidden = True 
    End If 

    If Range("C329").Value = "Unused" Then 
     Range("CMarket4").EntireRow.Hidden = True 
    End If 

    If Range("C437").Value = "Unused" Then 
     Range("CMarket5").EntireRow.Hidden = True 
    End If 

    If Range("C545").Value = "Unused" Then 
     Range("CMarket6").EntireRow.Hidden = True 
    End If 

    If Range("C653").Value = "Unused" Then 
     Range("CMarket7").EntireRow.Hidden = True 
    End If 

    If Range("C761").Value = "Unused" Then 
     Range("CMarket8").EntireRow.Hidden = True 
    End If 

    If Range("C869").Value = "Unused" Then 
     Range("CMarket9").EntireRow.Hidden = True 
    End If 

    If Range("C977").Value = "Unused" Then 
     Range("CMarket10").EntireRow.Hidden = True 
    End If 

    For Each C In Range("CNonTest") 
     If C.Value = "" And C.Columns(41).Value = "" Then 
      C.EntireRow.Hidden = True 
     End If 
    Next 



    Range("CBlank").EntireRow.Hidden = True 

    End With 
End Sub 

: 아래

는 내가 현재 가지고 코드 나 또한 아무 소용이 시도 한 시트 코드를

Private Sub Worksheet_Change(ByVal Target As Range) 

If Intersect(Target, Me.Range("A4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("D4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("G4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("K4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("AO4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("AR4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("AU4")) Is Nothing _ 
    Or _ 
    Intersect(Target, Me.Range("AY4")) Is Nothing _ 
    Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 


End Sub 

Private Sub Worksheet_Change(ByVal Target As Range) 

If Intersect(Target, Me.Range("A4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 



If Intersect(Target, Me.Range("D4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 



If Intersect(Target, Me.Range("G4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 



If Intersect(Target, Me.Range("K4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 



If Intersect(Target, Me.Range("AO4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 



If Intersect(Target, Me.Range("AR4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 



If Intersect(Target, Me.Range("AU4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 


If Intersect(Target, Me.Range("AY4")) Is Nothing Then Exit Sub 

    Application.EnableEvents = False 'to prevent endless loop 
    Application.ScreenUpdating = False 

    Call CompHide 

    Application.ScreenUpdating = True 
    Application.EnableEvents = True 


End Sub 

이 코드는 모두 정상적으로 작동하며 F9 키를 사용하여 CompHide를 단계별로 실행하면 완벽하게 작동합니다. 그래서 문제는 시트 자체의 코드에서 나온 것이라고 생각합니다. 그 코드에서 무한 루프를 막기 위해 언급 된 주석을 보게 될 것입니다. 그 주석은 어떤 코드인지 아닌지를 알려주는 코드입니다.

유효성 검사 상자를 변경하면 모든 항목이 더 이상 숨겨지지 않습니다. 다행히도 아직까지는 생각지도 못했던 것을 숨기는 것을 보지 못했습니다. 내가 처음이 코드는 첫 번째 검증 상자를 쳐다 보면서 더 이상 때문에에 말을하지만 지금은 이벤트 처리기에 대한 모든 8.

답변

4

일부 조정 본다 :

Private Sub Worksheet_Change(ByVal Target As Range) 

    Dim rng As Range 

    On Error GoTo haveError 

    Set rng = Application.Intersect(Target, Me.Range("A4,D4,G4,K4,AO4,AR4,AU4,AY4")) 

    If Not rng Is Nothing Then 
     Application.EnableEvents = False 'to prevent endless loop 
     Application.ScreenUpdating = False 
     CompHide 
     Application.EnableEvents = True 
    End If 
    Exit Sub 

haveError: 
    'always re-enable events 
    ' (screenupdating setting is not persistent)... 
    Application.EnableEvents = True 

End Sub 

과 다른 부분 :

Sub CompHide() 

    Dim sht As Worksheet, C As Range 

    Set sht = Sheets("Comparison") 
    sht.Rows.Hidden = False 

    SetRowVis "C9", "CMarket1" 
    SetRowVis "C115", "CMarket2" 
    '...and the rest 

    For Each C In sht.Range("CNonTest") 
     If C.Value = "" And C.EntireRow.Columns(43).Value = "" Then 
      C.EntireRow.Hidden = True 
     End If 
    Next 

    sht.Range("CBlank").EntireRow.Hidden = True 
End Sub 

'utility sub... 
Sub SetRowVis(addr As String, rngName As String) 
    With Sheets("Comparison") 
     If .Range(addr).Value = "Unused" Then 
      .Range(rngName).EntireRow.Hidden = True 
     End If 
    End With 
End Sub 
+1

시작 범위와 관계없이'.EntireRow.Columns (43)'을 사용하여 칼럼 43을 참조하는 것은 꽤 영리합니다. –

+0

금요일에 사무실로 돌아올 때 시험 할 도움을 보내 주셔서 감사합니다. 그러나 그것을 읽는 것은 완벽한 의미가된다. –

+0

또한 @Thomas와 동의한다. .entirerow.columns (43)는 감미로운 생각이었다. –

1

첫째로, 귀하는 CompHide Sub에 대한 참조 문제가 있습니다.
워크 시트에 대한 Range 개체 호출을 모두 완전히 참조해야합니다.

With Sheets("Comparison") 
    .Cells.EntireRow.Hidden = False 
    'Notice the dot in front of the Range object 
    If .Range("C9").Value = "Unused" Then .Range("CMarket1").EntireRow.Hidden = True 
    'Also notice that I used a one liner IF which I think is applicable for you 
    'Rest of your code go here 
    '. 
    '. 
    '. 

End With 

두 번째로 Tim의 게시물을 살펴보십시오. 그는 그것에 나를 때린다. :)

+0

그가 당신을 때렸다 고해도 고마워요. –