1
votes

New to VBA and trying to create a code to hide/unhide rows on a sheet 2 (named Charge Codes) based on a value of a cell in sheet 1 (F4) (Values 0-10). I'm currently using the code below and it works through cell F23, but then the procedure is too long. I need to replicate this for cells F4:F53, with each of those F cells having a different corresponding set of 10 rows to hide/unhide on sheet 2. Can anyone help me compile this? This is the example I'm using for F4:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = ("$F$4") And Target.Value = "0" Then Sheets(“Charge Codes”).Rows("3:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "1" Then Sheets(“Charge Codes”).Rows("3").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("4:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "2" Then Sheets(“Charge Codes”).Rows("3:4").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("5:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "3" Then Sheets(“Charge Codes”).Rows("3:5").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("6:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "4" Then Sheets(“Charge Codes”).Rows("3:6").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("7:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "5" Then Sheets(“Charge Codes”).Rows("3:7").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("8:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "6" Then Sheets(“Charge Codes”).Rows("3:8").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("9:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "7" Then Sheets(“Charge Codes”).Rows("3:9").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("10:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "8" Then Sheets(“Charge Codes”).Rows("3:10").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("11:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "9" Then Sheets(“Charge Codes”).Rows("3:11").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("12:12").Hidden = True

If Target.Address = ("$F$4") And Target.Value = "10" Then Sheets(“Charge Codes”).Rows("3:12").EntireRow.Hidden = False

End Sub
3
Is this on the Worksheets("Charge Codes") code sheet? i.e. Is Target on Worksheets("Charge Codes")?user4039065

3 Answers

1
votes

If I got my basic maths correct this should take care of your groups of hidden rows.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$F$4" Then
        Worksheets("Charge Codes").Cells(3, 1).Resize(10, 1).EntireRow.Hidden = False
        Select Case Target.Value
            Case 0, 10
                Worksheets("Charge Codes").Cells(3, 1).Resize(10, 1).EntireRow.Hidden = True
            Case 1, 2, 3, 4, 5, 6, 7, 8, 9
                Worksheets("Charge Codes").Cells(3, 1).Resize(Target.Value, 1).EntireRow.Hidden = True
            Case Else
                'do nothing
        End Select
    End If

End Sub
1
votes

This uses nested Ifs:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = ("$F$4") Then

    If Target.Value <> 0 Then
        Sheets("Charge Codes").Rows("3:12").Hidden = True
    ElseIf Target.Value = 10 Then
        Sheets("Charge Codes").Rows("3:12").Hidden = False
    Else
        Sheets("Charge Codes").Rows(3 + Target.Value & ":12").Hidden = True
        Sheets("Charge Codes").Rows("3:" & 2 + Target.Value).Hidden = False
    End If

End Sub
0
votes

Just use a logic to find the row numbers to hide

If Target.Address = ("$F$4") Then
Sheets("Charge Codes").Rows(3 & ":" & CDbl(Target.Value) + (CDbl(Target.Value) - 3)).EntireRow.Hidden = False: _
Sheets("Charge Codes").Rows(12 - CDbl(Target.Value) & ":" & 12).EntireRow.Hidden = True
End If