2017-04-14 12 views
0

VBAオブジェクトを作成して、特定の値が選択された場合にセルを非表示にします。これはすべて第1列に含まれています。ドロップダウンに基づいてセルを非表示にします。選択しないとアクティブになります。

しかし、私が情報を入力すると他の列を編集し続けるたびに、私はすべてのものを隠します。

完全なコードは以下のとおりです。主に同じことが5回以上繰り返されています。ありがとう!

Private Sub Worksheet_Change(ByVal Target As Range) 
 
    If Target.Column = 1 And Target.Row = 3 And Target.Value = "Cashback" Then 
 
     Application.Rows("4:7").Select 
 
     Application.Selection.EntireRow.Hidden = False 
 
    Else 
 
     Application.Rows("4:7").Select 
 
     Application.Selection.EntireRow.Hidden = True 
 
    End If 
 
    
 
     If Target.Column = 1 And Target.Row = 3 And Target.Value = "Content" Then 
 
     Application.Rows("8:25").Select 
 
     Application.Selection.EntireRow.Hidden = False 
 
    Else 
 
     Application.Rows("8:25").Select 
 
     Application.Selection.EntireRow.Hidden = True 
 
    End If 
 

 
    If Target.Column = 1 And Target.Row = 3 And Target.Value = "Price Comparison" Then 
 
     Application.Rows("26:40").Select 
 
     Application.Selection.EntireRow.Hidden = False 
 
    Else 
 
     Application.Rows("26:40").Select 
 
     Application.Selection.EntireRow.Hidden = True 
 
    End If 
 
    
 
    If Target.Column = 1 And Target.Row = 3 And Target.Value = "Technology" Then 
 
     Application.Rows("41:52").Select 
 
     Application.Selection.EntireRow.Hidden = False 
 
    Else 
 
     Application.Rows("41:52").Select 
 
     Application.Selection.EntireRow.Hidden = True 
 
    End If 
 
    
 
     If Target.Column = 1 And Target.Row = 3 And Target.Value = "Vouchers" Then 
 
     Application.Rows("53:79").Select 
 
     Application.Selection.EntireRow.Hidden = False 
 
    Else 
 
     Application.Rows("53:79").Select 
 
     Application.Selection.EntireRow.Hidden = True 
 
    End If 
 
    
 
      If Target.Column = 1 And Target.Row = 3 And Target.Value = "All" Then 
 
     Application.Rows("3:200").Select 
 
     Application.Selection.EntireRow.Hidden = False 
 
    End If 
 
    
 
    
 

 
End Sub

答えて

0

...このようにそれを試してみてください私は、最も重要なのは、それがより効率的にするために、コードを少しリファクタリング維持/理解しやすい、とその要件を満たす。

Option Explicit 

Private Sub Worksheet_Change(ByVal Target As Range) 

If Not Intersect(Target, Me.Range("A3")) Is Nothing and Target.Cells.Count = 1 Then 

    Application.ScreenUpdating = False 

    Me.Rows("4:200").EntireRow.Hidden = True 

    Select Case Target.Value 

     Case Is = "Cashback": Me.Rows("4:7").EntireRow.Hidden = False 
     Case Is = "Content": Me.Rows("8:25").EntireRow.Hidden = False 
     Case Is = "Price Comparison": Me.Rows("26:40").EntireRow.Hidden = False 

     '... Continue with rest of scenarios ... 

     Case Is = "All": Me.Rows("4:200").EntireRow.Hidden = False 

    End Select 

End If 


End Sub 
+0

これは私が探していたものをクリアし、実装しようとしていたコードのはるかに賢明な使い方です。ありがとうございました。 –

0

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.CountLarge > 1 Then Exit Sub 

    If Target.Column = 1 And Target.Row = 3 Then 
     If Target.Value = "Cashback" Then 
      Rows("4:7").EntireRow.Hidden = False 
     Else 
      Application.Rows("4:7").Select 
      Rows("4:7").EntireRow.Hidden = True 
     End If 

     If Target.Value = "Content" Then 
      Rows("8:25").EntireRow.Hidden = False 
     Else 
      Rows("8:25").EntireRow.Hidden = True 
     End If 

     If Target.Value = "Price Comparison" Then 
      Rows("26:40").EntireRow.Hidden = False 
     Else 
      Rows("26:40").EntireRow.Hidden = True 
     End If 

     If Target.Value = "Technology" Then 
      Rows("41:52").EntireRow.Hidden = False 
     Else 
      Rows("41:52").EntireRow.Hidden = True 
     End If 

     If Target.Value = "Vouchers" Then 
      Rows("53:79").EntireRow.Hidden = False 
     Else 
      Rows("53:79").EntireRow.Hidden = True 
     End If 

     If Target.Value = "All" Then 
      Rows("3:200").EntireRow.Hidden = False 
     End If 

    End If 

End Sub 
0

問題はSelect文のElseです。このコードを試してください。

Private Sub Worksheet_Change(ByVal Target As Range) 
    ' 14 Apr 2017 

    Dim Rng As Range 

    With Target 
     If .Address = Cells(3, 1).Address Then 
      Application.ScreenUpdating = False 
      Set Rng = Range.Rows("3:200") 
      If .Value <> "All" Then 
       Rng.Hidden = True 
       Select Case .Value 
        Case "Cashback" 
         Set Rng = Rows("4:7") 
        Case "Content" 
         Set Rng = Rows("8:25") 
        Case "Price Comparison" 
         Set Rng = Rows("26:40") 
        Case "Technology" 
         Set Rng = Rows("41:52") 
        Case "Vouchers" 
         Set Rng = Rows("53:79") 
       End Select 
      End If 
      Rng.Hidden = False 
      Rng.Select 
      Application.ScreenUpdating = False 
     End If 
    End With 
End Sub 
関連する問題