Problem with worksheet hiding/unhiding despite using unprotect?

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
I'm getting the error:

0ZTb1ut.png


Debug says it relates to the following line:
VBA Code:
Range("21:34").EntireRow.Hidden = True

Not sure why?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
'MsgBox "The range that changed was" & vbLf & Target.Address(0, 0)    'for test purposes only
    
    Select Case Target.Address(0, 0)
        Case "G16"
            ActiveSheet.Shapes("CheckBox6").Visible = (Len(Target.Value) > 0)
        Case "C10"
            'Application.EnableEvents = False
        Sheets("Instalments").Unprotect
            If Target.Value = "No Payments" Then
                Range("F:F").EntireColumn.Hidden = True
            ElseIf Target.Value = "Credit on Account" Then
                Range("F:F").EntireColumn.Hidden = False
            ElseIf Target.Value = "Debit on Account" Then
                Range("F:F").EntireColumn.Hidden = False
            End If
        Case "C20"
            If Target.Value = "No Discount" Then
                Range("21:34").EntireRow.Hidden = True
                Range("C20").Select
            ElseIf Target.Value = "25% Discount" Then
                Range("21:24").EntireRow.Hidden = False
                Range("25:34").EntireRow.Hidden = True
            ElseIf Target.Value = "50% Discount" Then
                Range("26:29").EntireRow.Hidden = False
                Range("21:25").EntireRow.Hidden = True
                Range("30:34").EntireRow.Hidden = True
            ElseIf Target.Value = "50% Discount & 25% Discount" Then
                Range("31:34").EntireRow.Hidden = False
                Range("21:30").EntireRow.Hidden = True
            End If
            Sheets("Instalments").Protect
            Range("C20").Select
    End Select
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Your .Unprotect statement is sitting inside one particular case:

VBA Code:
'...

Case "C10"
    Sheets("Instalments").Unprotect
            
'...
 
Upvote 0
Solution
Your .Unprotect statement is sitting inside one particular case:

VBA Code:
'...

Case "C10"
    Sheets("Instalments").Unprotect
           
'...
Thank you.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
        Sheets("Instalments").Unprotect
    Select Case Target.Address(0, 0)
        Case "G16"
            ActiveSheet.Shapes("CheckBox6").Visible = (Len(Target.Value) > 0)
        Case "C10"
            If Target.Value = "No Payments" Then
                Range("F:F").EntireColumn.Hidden = True
            ElseIf Target.Value = "Credit on Account" Then
                Range("F:F").EntireColumn.Hidden = False
            ElseIf Target.Value = "Debit on Account" Then
                Range("F:F").EntireColumn.Hidden = False
            End If
        Case "C20"
            If Target.Value = "No Discount" Then
                Range("21:34").EntireRow.Hidden = True
                Range("C20").Select
            ElseIf Target.Value = "25% Discount" Then
                Range("21:24").EntireRow.Hidden = False
                Range("25:34").EntireRow.Hidden = True
            ElseIf Target.Value = "50% Discount" Then
                Range("26:29").EntireRow.Hidden = False
                Range("21:25").EntireRow.Hidden = True
                Range("30:34").EntireRow.Hidden = True
            ElseIf Target.Value = "50% Discount & 25% Discount" Then
                Range("31:34").EntireRow.Hidden = False
                Range("21:30").EntireRow.Hidden = True
            End If
            Sheets("Instalments").Protect
            Range("C20").Select
    End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,768
Members
449,122
Latest member
sampak88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top