Trying to hide rows in Excel based on certain cell values - vba

I am looking around for a formula to hide certain rows based on certain cell inputs. In cell C5 I have a drop-down selection of "Corporates" and "Projects". In cell C8 I have a drop-down selection of "High", "Medium", and "Low". In cell H6 I have the formula =C5&C8. The macro I have is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "H6" Then
Select Case Target.Value
Case "CorporatesHigh": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False
Case "CorporatesMedium": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False
Case "CorporatesLow": Rows("25:33").Hidden = True: Rows("12:24").Hidden = False
Case "ProjectsHigh": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True
Case "ProjectsMedium": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True
Case "ProjectsLow": Rows("25:33").Hidden = False: Rows("12:24").Hidden = True
Case "": Rows("12:33").Hidden = False
Case "Corporates": Rows("12:33").Hidden = False
Case "Projects": Rows("12:33").Hidden = False
Case "High": Rows("12:33").Hidden = False
Case "Medium": Rows("12:33").Hidden = False
Case "Low": Rows("12:33").Hidden = False
End Select
End If
End Sub
The macro works when I click into H6 but I need it to work when cells C5 or C8 is changed.

The If-Not-Intersect-Is Nothing pattern is the preferred way to check if the changed cells(s) are inside a given range of cell(s).
To handle all three of your cells, you can use the following. You can easily add or remove cells to the Range if you want.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H6,C5,C8")) Is Nothing Then
Select Case Range("H6").Value
Case "CorporatesHigh": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False
Case "CorporatesMedium": Rows("21:33").Hidden = True: Rows("12:20").Hidden = False
Case "CorporatesLow": Rows("25:33").Hidden = True: Rows("12:24").Hidden = False
Case "ProjectsHigh": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True
Case "ProjectsMedium": Rows("25:28").Hidden = False: Rows("29:33").Hidden = True: Rows("12:24").Hidden = True
Case "ProjectsLow": Rows("25:33").Hidden = False: Rows("12:24").Hidden = True
Case "": Rows("12:33").Hidden = False
Case "Corporates": Rows("12:33").Hidden = False
Case "Projects": Rows("12:33").Hidden = False
Case "High": Rows("12:33").Hidden = False
Case "Medium": Rows("12:33").Hidden = False
Case "Low": Rows("12:33").Hidden = False
End Select
End If
End Sub

Related

VBA Procedure too large; having trouble running a sub procedure

I've divided an excel worksheet into different sections, each section being 20 rows in size (example: Section 1 = rows 132 to 152, Section 2 = rows 153 to 173, etc.). There are 100 sections. I'm running a procedure in VBA that, for each section, will hide a certain number of rows based on a desired number of rows for each section. After about running the routine on 23 sections, I get a "Procedure too Large" error. I was told that I should run a sub procedure to solve the problem. I'm not sure how to do that. I'm using Sub proc1(), but it's not working. Here is the first part of the code, followed by the Sub proc1(). It's not working. I'm now getting the following error immediately after the Sup proc1() line:
Ambiguous name detected: Worksheet_Change
If anyone can help me with the proper code after the Sub proc1() that would help immensely!
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect
ActiveSheet.Activate
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "0": Rows("132:152").EntireRow.Hidden = True
Case Is = "1": Rows("134:152").EntireRow.Hidden = True
Rows("123:133").EntireRow.Hidden = False
Case Is = "2": Rows("135:152").EntireRow.Hidden = True
Rows("123:134").EntireRow.Hidden = False
Case Is = "3": Rows("136:152").EntireRow.Hidden = True
Rows("123:135").EntireRow.Hidden = False
Case Is = "4": Rows("137:152").EntireRow.Hidden = True
Rows("123:136").EntireRow.Hidden = False
Case Is = "5": Rows("138:152").EntireRow.Hidden = True
Rows("123:137").EntireRow.Hidden = False
Case Is = "6": Rows("139:152").EntireRow.Hidden = True
Rows("123:138").EntireRow.Hidden = False
Case Is = "7": Rows("140:152").EntireRow.Hidden = True
Rows("123:139").EntireRow.Hidden = False
Case Is = "8": Rows("141:152").EntireRow.Hidden = True
Rows("123:140").EntireRow.Hidden = False
Case Is = "9": Rows("142:152").EntireRow.Hidden = True
Rows("123:141").EntireRow.Hidden = False
Case Is = "10": Rows("143:152").EntireRow.Hidden = True
Rows("123:142").EntireRow.Hidden = False
Case Is = "11": Rows("144:152").EntireRow.Hidden = True
Rows("123:143").EntireRow.Hidden = False
Case Is = "12": Rows("145:152").EntireRow.Hidden = True
Rows("123:144").EntireRow.Hidden = False
Case Is = "13": Rows("146:152").EntireRow.Hidden = True
Rows("123:145").EntireRow.Hidden = False
Case Is = "14": Rows("147:152").EntireRow.Hidden = True
Rows("123:146").EntireRow.Hidden = False
Case Is = "15": Rows("148:152").EntireRow.Hidden = True
Rows("123:147").EntireRow.Hidden = False
Case Is = "16": Rows("149:152").EntireRow.Hidden = True
Rows("123:148").EntireRow.Hidden = False
Case Is = "17": Rows("150:152").EntireRow.Hidden = True
Rows("123:149").EntireRow.Hidden = False
Case Is = "18": Rows("151:152").EntireRow.Hidden = True
Rows("123:150").EntireRow.Hidden = False
Case Is = "19": Rows("152:152").EntireRow.Hidden = True
Rows("123:151").EntireRow.Hidden = False
Case Is = "20": Rows("123:152").EntireRow.Hidden = False
End Select
End If
and so on...then:
If Not Application.Intersect(Range("G43"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "0": Rows("615:635").EntireRow.Hidden = True
Case Is = "1": Rows("617:635").EntireRow.Hidden = True
Rows("615:616").EntireRow.Hidden = False
Case Is = "2": Rows("618:635").EntireRow.Hidden = True
Rows("615:617").EntireRow.Hidden = False
Case Is = "3": Rows("619:635").EntireRow.Hidden = True
Rows("615:618").EntireRow.Hidden = False
Case Is = "4": Rows("620:635").EntireRow.Hidden = True
Rows("615:619").EntireRow.Hidden = False
Case Is = "5": Rows("621:635").EntireRow.Hidden = True
Rows("615:620").EntireRow.Hidden = False
Case Is = "6": Rows("622:635").EntireRow.Hidden = True
Rows("615:621").EntireRow.Hidden = False
Case Is = "7": Rows("623:635").EntireRow.Hidden = True
Rows("615:622").EntireRow.Hidden = False
Case Is = "8": Rows("624:635").EntireRow.Hidden = True
Rows("615:623").EntireRow.Hidden = False
Case Is = "9": Rows("625:635").EntireRow.Hidden = True
Rows("615:624").EntireRow.Hidden = False
Case Is = "10": Rows("626:635").EntireRow.Hidden = True
Rows("615:625").EntireRow.Hidden = False
Case Is = "11": Rows("627:635").EntireRow.Hidden = True
Rows("615:626").EntireRow.Hidden = False
Case Is = "12": Rows("628:635").EntireRow.Hidden = True
Rows("615:627").EntireRow.Hidden = False
Case Is = "13": Rows("629:635").EntireRow.Hidden = True
Rows("615:628").EntireRow.Hidden = False
Case Is = "14": Rows("630:635").EntireRow.Hidden = True
Rows("615:629").EntireRow.Hidden = False
Case Is = "15": Rows("631:635").EntireRow.Hidden = True
Rows("615:630").EntireRow.Hidden = False
Case Is = "16": Rows("632:635").EntireRow.Hidden = True
Rows("615:631").EntireRow.Hidden = False
Case Is = "17": Rows("633:635").EntireRow.Hidden = True
Rows("615:632").EntireRow.Hidden = False
Case Is = "18": Rows("634:635").EntireRow.Hidden = True
Rows("615:633").EntireRow.Hidden = False
Case Is = "19": Rows("635:635").EntireRow.Hidden = True
Rows("615:634").EntireRow.Hidden = False
Case Is = "20": Rows("615:635").EntireRow.Hidden = False
End Select
End If
Call proc1
Call proc2
End Sub
Sub proc1()
If Not Application.Intersect(Range("G44"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "0": Rows("636:656").EntireRow.Hidden = True
Case Is = "1": Rows("638:656").EntireRow.Hidden = True
Rows("636:637").EntireRow.Hidden = False
Case Is = "2": Rows("639:656").EntireRow.Hidden = True
Rows("636:638").EntireRow.Hidden = False
Case Is = "3": Rows("640:656").EntireRow.Hidden = True
Rows("636:639").EntireRow.Hidden = False
Case Is = "4": Rows("641:656").EntireRow.Hidden = True
Rows("636:640").EntireRow.Hidden = False
Case Is = "5": Rows("642:656").EntireRow.Hidden = True
Rows("636:641").EntireRow.Hidden = False
Case Is = "6": Rows("643:656").EntireRow.Hidden = True
Rows("636:642").EntireRow.Hidden = False
Case Is = "7": Rows("644:656").EntireRow.Hidden = True
Rows("636:643").EntireRow.Hidden = False
Case Is = "8": Rows("645:656").EntireRow.Hidden = True
Rows("636:644").EntireRow.Hidden = False
Case Is = "9": Rows("646:656").EntireRow.Hidden = True
Rows("636:645").EntireRow.Hidden = False
Case Is = "10": Rows("647:656").EntireRow.Hidden = True
Rows("636:646").EntireRow.Hidden = False
Case Is = "11": Rows("648:656").EntireRow.Hidden = True
Rows("636:647").EntireRow.Hidden = False
Case Is = "12": Rows("649:656").EntireRow.Hidden = True
Rows("636:648").EntireRow.Hidden = False
....and so on
You can use a loop and a some math to reduce your code a lot:
Private Sub Worksheet_Change(ByVal Target As Range)
Const BLOCK_SIZE As Long = 20 '(+1 header)
Dim n As Long, i As Long, rngCheck As Range, c As Range, rStart As Long
Me.Unprotect 'use Me to refer to the sheet itself
Set rngCheck = Me.Range("G20") 'first cell to check
rStart = 132 'start row (header) for section 1
For i = 1 To 100
Set c = Application.Intersect(rngCheck, Target) 'Target intersects with cell being checked?
If Not c Is Nothing Then
n = c.Value
If n > BLOCK_SIZE Then n = BLOCK_SIZE 'limit number of rows to show
Me.Cells(rStart, 1).Resize(BLOCK_SIZE + 1).EntireRow.Hidden = True 'hide all
If n > 0 Then Me.Cells(rStart, 1).Resize(n + 1).EntireRow.Hidden = False 'unhide the ones we want
End If
Set rngCheck = rngCheck.Offset(1, 0) 'next cell to check
rStart = rStart + (BLOCK_SIZE + 1) 'next block starting row
Next i
End Sub

hide/unhide many textboxes on a form based on combo value in ms-access (userform)

I currently have too many if statements; what is the best solution to coding hiding and unhiding about textboxes, I think I have 8 or more combinations here is the current code I have when trying this it only works with the last if statement combinations are as follows A-D, and all combinations between.:
If Combo308.Value = "A" Then
Combo305.Visible = True
Option103.Visible = True
Else
Combo305.Visible = False
Option103.Visible = False
End If
If Combo308.Value = "B" Then
Combo306.Visible = True
Option105.Visible = True
Else
Combo306.Visible = False
Option105.Visible = False
End If
If Combo308.Value = "D" Then
Combo309.Visible = True
Option111.Visible = True
Text310.Visible = True
Option113.Visible = True
Combo311.Visible = True
Option115.Visible = True
Text312.Visible = True
Option117.Visible = True
Text313.Visible = True
Option119.Visible = True
Combo314.Visible = True
Option121.Visible = True
Text315.Visible = True
Option123.Visible = True
Text316.Visible = True
Option125.Visible = True
Text317.Visible = True
Option127.Visible = True
Text318.Visible = True
Option129.Visible = True
Text319.Visible = True
Option131.Visible = True
Else
Combo309.Visible = False
Option111.Visible = False
Text310.Visible = False
Option113.Visible = False
Combo311.Visible = False
Option115.Visible = False
Text312.Visible = False
Option117.Visible = False
Text313.Visible = False
Option119.Visible = False
Combo314.Visible = False
Option121.Visible = False
Text315.Visible = False
Option123.Visible = False
Text316.Visible = False
Option125.Visible = False
Text317.Visible = False
Option127.Visible = False
Text318.Visible = False
Option129.Visible = False
Text319.Visible = False
Option131.Visible = False
End If
If Combo308.Value = "A,B" Then
Combo305.Visible = True
Option103.Visible = True
Combo306.Visible = True
Option105.Visible = True
Else
Combo305.Visible = False
Option103.Visible = False
Combo306.Visible = False
Option105.Visible = False
End If
If Combo308.Value = "A,B,C" Then
Combo305.Visible = True
Option103.Visible = True
Combo306.Visible = True
Option105.Visible = True
Combo307.Visible = True
Option109.Visible = True
Else
Combo305.Visible = False
Option103.Visible = False
Combo306.Visible = False
Option105.Visible = False
Combo307.Visible = False
Option109.Visible = False
End If
I guess you need first hide all controls, then unhide a selection:
Combo305.Visible = False
Option103.Visible = False
' etc. - all controls in question.
Select Case Combo308.Value
Case "A"
Combo305.Visible = True
Option103.Visible = True
Case "B"
Combo306.Visible = True
Option105.Visible = True
Case "D"
' etc.
'
Case "A,B,C"
Combo305.Visible = True
Option103.Visible = True
Combo306.Visible = True
Option105.Visible = True
Combo307.Visible = True
Option109.Visible = True
End Select
And do rename all your controls to have meaningful names.

Group Box content - hide based on cell value

I have the following code:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Select Case Range("B14")
Case "Medium Risk"
Rows("6:12").EntireRow.Hidden = True
Case Else
Rows("6:12").EntireRow.Hidden = False
End Select
Application.EnableEvents = True
End Sub
What I need is: to hide Group Box "content" based on the value in B14.
Group Box 15 surrounds a CheckBox8_Click.
Any ideas?
Thanks
I decided that I will simply settle with hiding a row [based on cell criteria] where the object is:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Select Case Range("B14")
Case "Medium Risk"
Rows("6:12").EntireRow.Hidden = True
Rows("24").EntireRow.Hidden = True
Case Else
Rows("6:12").EntireRow.Hidden = False
Rows("24").EntireRow.Hidden = False
End Select
Application.EnableEvents = True
End Sub

If Else VBA Issue

My problem is that my code does not pick up the "Medium" variable
If Range("B24").Value = "Medium" Then
Sheets("Sheet2").Visible = True
Can you please advise what's wrong with it ("Standard" should uncover rows, "Medium" should keep them hidden but unhide the Sheet2, High again keeps the rows hidden but unhides Sheet2, if (B24) is empty it should keep rows and Sheet2 hidden:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B24").Value = "Standard" Then
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Medium" Then
Sheets("Sheet2").Visible = True
End If
If Range("B24").Value = "High" Then
Sheets("Sheet2").Visible = True
End If
Else
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Standard" Then
Rows("29:47").EntireRow.Hidden = False
Else
Rows("29:47").EntireRow.Hidden = True
End If
End Sub
You are obviously missing an If, and your code should not be working. This is how it looks like, when it is formatted:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B24").Value = "Standard" Then
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Medium" Then
Sheets("Sheet2").Visible = True
End If
If Range("B24").Value = "High" Then
Sheets("Sheet2").Visible = True
End If
'Is this Else an IF?
Else
Sheets("Sheet2").Visible = False
End If
If Range("B24").Value = "Standard" Then
Rows("29:47").EntireRow.Hidden = False
Else
Rows("29:47").EntireRow.Hidden = True
End If
End Sub
If you omit the If-Else, the code would look a bit better with a Select-Case statement:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("B24")
Case "Standard"
Sheets("Sheet2").Visible = False
Rows("29:47").EntireRow.Hidden = False
Case "Medium"
Sheets("Sheet2").Visible = True
Rows("29:47").EntireRow.Hidden = True
Case "High"
Sheets("Sheet2").Visible = True
Rows("29:47").EntireRow.Hidden = True
Case Else
Sheets("Sheet2").Visible = False
Rows("29:47").EntireRow.Hidden = True
End Select
End Sub
You can probably go one step further, by writing Rows("29:47").EntireRow.Hidden = True before the select case and remove this line from cases, thus following the Do-Not-Repeat-Yourself principle. But in this case it is probably not needed.
If you want to add a second range, something like this is possible:
Select Case True
Case Range("B24") = "Standard"
Case Range("B26") = "Medium"
End Select
However, it would evaluate just once, thus if both are true, B26 is not going to happen.

Looking for ways to simplify my code

I'm having trouble adding another function in that macro I used to activate or deactivate columns in a Excel Workbook we are using at work. I'm getting the Out of stack space error when running it just adding one of the following function.
Mostly, I've used an If( ;1;0) to manage the activation part and an If(;TRUE;FALSE) for the locked/unlocked part. The function I want to had is based on the same idea using a verification code to Clearcontents of a cell and locked it. If the verification code is false, then, I want the cell to be unlocked so the user can write the value. Here is the code line I want to had times 15 as already done for the locked, unlocked function.
If Range("AS16") = "Vrai" Then
Range("AA16").ClearContents
Range("AA16").Locked = True
Else:
Range("AA16").Locked = False
End If
Here is the code I'm using right now.
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect ("Francis")
Dim Cell As Range
Set Cell = ActiveCell
Application.ScreenUpdating = False
For Each cellule In Range("S50:X50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("S50:X50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
If Range("AR16") = "Vrai" Then
Range("K16").Locked = False
Range("O16").Locked = False
Else:
Range("K16").Locked = True
Range("O16").Locked = True
End If
If Range("AR18") = "Vrai" Then
Range("K18").Locked = False
Range("O18").Locked = False
Else:
Range("K18").Locked = True
Range("O18").Locked = True
End If
If Range("AR20") = "Vrai" Then
Range("K20").Locked = False
Range("O20").Locked = False
Else:
Range("K20").Locked = True
Range("O20").Locked = True
End If
If Range("AR22") = "Vrai" Then
Range("K22").Locked = False
Range("O22").Locked = False
Else:
Range("K22").Locked = True
Range("O22").Locked = True
End If
If Range("AR24") = "Vrai" Then
Range("K24").Locked = False
Range("O24").Locked = False
Else:
Range("K24").Locked = True
Range("O24").Locked = True
End If
If Range("AR26") = "Vrai" Then
Range("K26").Locked = False
Range("O26").Locked = False
Else:
Range("K26").Locked = True
Range("O26").Locked = True
End If
If Range("AR28") = "Vrai" Then
Range("K28").Locked = False
Range("O28").Locked = False
Else:
Range("K28").Locked = True
Range("O28").Locked = True
End If
If Range("AR30") = "Vrai" Then
Range("K30").Locked = False
Range("O30").Locked = False
Else:
Range("K30").Locked = True
Range("O30").Locked = True
End If
If Range("AR32") = "Vrai" Then
Range("K32").Locked = False
Range("O32").Locked = False
Else:
Range("K32").Locked = True
Range("O32").Locked = True
End If
If Range("AR34") = "Vrai" Then
Range("K34").Locked = False
Range("O34").Locked = False
Else:
Range("K34").Locked = True
Range("O34").Locked = True
End If
If Range("AR36") = "Vrai" Then
Range("K36").Locked = False
Range("O36").Locked = False
Else:
Range("K36").Locked = True
Range("O36").Locked = True
End If
If Range("AR38") = "Vrai" Then
Range("K38").Locked = False
Range("O38").Locked = False
Else:
Range("K38").Locked = True
Range("O38").Locked = True
End If
If Range("AR40") = "Vrai" Then
Range("K40").Locked = False
Range("O40").Locked = False
Else:
Range("K40").Locked = True
Range("O40").Locked = True
End If
If Range("AR42") = "Vrai" Then
Range("K42").Locked = False
Range("O42").Locked = False
Else:
Range("K42").Locked = True
Range("O42").Locked = True
End If
If Range("AR44") = "Vrai" Then
Range("K44").Locked = False
Range("O44").Locked = False
Else:
Range("K44").Locked = True
Range("O44").Locked = True
End If
Application.ScreenUpdating = True
Application.Goto Cell
'ActiveSheet.Protect Password:="Francis"
End Sub
Thanks a lot for your help.
Have a nice day!
You typically don't want to have performance-expensive code running in that specific handler. Worksheet_Change gets invoked every time a cell changes... and that includes changing a cell's Locked property value.
So that's how you run out of stack space: your handler is modifying cells' Locked state, which triggers the Worksheet_Change event, which modifies cells' Locked state, which triggers the Worksheet_Change event, which modifies cells' Locked state, which triggers the Worksheet_Change event, which... which eventually blows the call stack.
So prevent this accidental recursion, you need to prevent Excel from firing worksheet events when you're handling one:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo CleanFail
Application.EnableEvents = False
'do stuff
CleanExit:
Application.EnableEvents = True
Exit Sub
CleanFail:
'handle errors here...
Resume CleanExit
End Sub
As for simplifying the code, that's more of a mandate for Code Review Stack Exchange, once your code works as intended.
If any C# dev is reading this, this particular situation now has an up-for-grabs issue on Rubberduck's GitHub repository: #3109 Prevent accidental recursion in Worksheet_Change and Workbook_SheetChange handlers; once that inspection is implemented, Rubberduck will be able to warn you when you handle Worksheet_Change without disabling application events.
The Out of stack error is caused by the Change event, as noted by #Mat
Try this version which also turns the events off and on
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cellule As Range, r As Long, isVrai As Boolean
ActiveSheet.Unprotect "Francis"
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
For Each cellule In Union(Range("I50:J50"), Range("S50:X50"))
With cellule
Select Case .Value2
Case "1": .EntireColumn.Hidden = False
Case "0": .EntireColumn.Hidden = True
End Select
End With
Next
For r = 16 To 44 Step 2
isVrai = (Range("AR" & r).Value2 = "Vrai")
Range("K" & r).Locked = Not isVrai
Range("O" & r).Locked = Not isVrai
If isVrai Then Range("AR" & r).ClearContents
Next
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'ActiveSheet.Protect Password:="Francis"
End Sub
This section can be simplified:
For Each cellule In Range("S50:X50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("S50:X50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "1" Then cellule.EntireColumn.Hidden = False
Next cellule
For Each cellule In Range("I50:J50")
If cellule.Value = "0" Then cellule.EntireColumn.Hidden = True
Next cellule
To the following (however, note that this will unhide any non-zero values).
For Each cellule in Range("S50:X50")
cellule.EntireColumn.Hidden = (cellule.Value = "0")
Next
For Each cellule in Range("I50:J50")
cellule.EntireColumn.Hidden = (cellule.Value = "0")
Next
And this section:
If Range("AR16") = "Vrai" Then
Range("K16").Locked = False
Range("O16").Locked = False
Else:
Range("K16").Locked = True
Range("O16").Locked = True
End If
If Range("AR18") = "Vrai" Then
Range("K18").Locked = False
Range("O18").Locked = False
Else:
Range("K18").Locked = True
Range("O18").Locked = True
End If
....
Can be simplified using a loop over Range("AR16:AR44")
For Each cellule in Range("AR16:AR44") Step 2
cellule.Offset(,-33).Locked = (cellule.Value = "Vrai")
cellule.Offset(,-29).Locked = (cellule.Value = "Vrai")
Next