I use the following code to hide and clear contents in cells based on cell values in another worksheet. Can I simplify this code?
Code:
Sub HideAndClear()
If Worksheets("Settings").Range("D2").Value = "r" Then
Worksheets("Salary").Range("G4:G57").ClearContents
Worksheets("Salary").Columns("G").Hidden = True
Else: Worksheets("Salary").Columns("G").Hidden = False
End If
If Worksheets("Settings").Range("D3").Value = "r" Then
Worksheets("Salary").Columns("H").Hidden = True
Else: Worksheets("Salary").Columns("H").Hidden = False
End If
If Worksheets("Settings").Range("D4").Value = "r" Then
Worksheets("Salary").Range("I4:I57").ClearContents
Worksheets("Salary").Columns("I").Hidden = True
Else: Worksheets("Salary").Columns("I").Hidden = False
End If
If Worksheets("Settings").Range("D5").Value = "r" Then
Worksheets("Salary").Range("J4:J57").ClearContents
Worksheets("Salary").Columns("J").Hidden = True
Else: Worksheets("Salary").Columns("J").Hidden = False
End If
If Worksheets("Settings").Range("D6").Value = "r" Then
Worksheets("Salary").Range("K4:K57").ClearContents
Worksheets("Salary").Columns("K").Hidden = True
Else: Worksheets("Salary").Columns("K").Hidden = False
End If
If Worksheets("Settings").Range("D7").Value = "r" Then
Worksheets("Salary").Range("L4:L57").ClearContents
Worksheets("Salary").Columns("L").Hidden = True
Else: Worksheets("Salary").Columns("L").Hidden = False
End If
If Worksheets("Settings").Range("D8").Value = "r" Then
Worksheets("Salary").Range("M4:M57").ClearContents
Worksheets("Salary").Columns("M").Hidden = True
Else: Worksheets("Salary").Columns("M").Hidden = False
End If
If Worksheets("Settings").Range("D9").Value = "r" Then
Worksheets("Salary").Range("N4:N57").ClearContents
Worksheets("Salary").Columns("N").Hidden = True
Else: Worksheets("Salary").Columns("N").Hidden = False
End If
If Worksheets("Settings").Range("D10").Value = "r" Then
Worksheets("Salary").Range("O4:O57").ClearContents
Worksheets("Salary").Columns("O").Hidden = True
Else: Worksheets("Salary").Columns("O").Hidden = False
End If
If Worksheets("Settings").Range("D11").Value = "r" Then
Worksheets("Salary").Range("Q4:Q57").ClearContents
Worksheets("Salary").Columns("Q").Hidden = True
Else: Worksheets("Salary").Columns("Q").Hidden = False
End If
If Worksheets("Settings").Range("D12").Value = "r" Then
Worksheets("Salary").Range("R4:R57").ClearContents
Worksheets("Salary").Columns("R").Hidden = True
Else: Worksheets("Salary").Columns("R").Hidden = False
End If
If Worksheets("Settings").Range("D13").Value = "r" Then
Worksheets("Salary").Range("S4:S57").ClearContents
Worksheets("Salary").Columns("S").Hidden = True
Else: Worksheets("Salary").Columns("S").Hidden = False
End If
If Worksheets("Settings").Range("D14").Value = "r" Then
Worksheets("Salary").Range("T4:T57").ClearContents
Worksheets("Salary").Columns("T").Hidden = True
Else: Worksheets("Salary").Columns("T").Hidden = False
End If
If Worksheets("Settings").Range("D15").Value = "r" Then
Worksheets("Salary").Range("U4:U57").ClearContents
Worksheets("Salary").Columns("U").Hidden = True
Else: Worksheets("Salary").Columns("U").Hidden = False
End If
If Worksheets("Settings").Range("D16").Value = "r" Then
Worksheets("Salary").Range("V4:V57").ClearContents
Worksheets("Salary").Columns("V").Hidden = True
Else: Worksheets("Salary").Columns("V").Hidden = False
End If
If Worksheets("Settings").Range("D17").Value = "r" Then
Worksheets("Salary").Range("W4:W57").ClearContents
Worksheets("Salary").Columns("W").Hidden = True
Else: Worksheets("Salary").Columns("W").Hidden = False
End If
If Worksheets("Settings").Range("D18").Value = "r" Then
Worksheets("Salary").Range("X4:X57").ClearContents
Worksheets("Salary").Columns("X").Hidden = True
Else: Worksheets("Salary").Columns("X").Hidden = False
End If
If Worksheets("Settings").Range("D19").Value = "r" Then
Worksheets("Salary").Range("Y4:Y57").ClearContents
Worksheets("Salary").Columns("Y").Hidden = True
Else: Worksheets("Salary").Columns("Y").Hidden = False
End If
If Worksheets("Settings").Range("D20").Value = "r" Then
Worksheets("Salary").Range("Z4:Z57").ClearContents
Worksheets("Salary").Columns("Z").Hidden = True
Else: Worksheets("Salary").Columns("Z").Hidden = False
End If
If Worksheets("Settings").Range("D21").Value = "r" Then
Worksheets("Salary").Range("AA4:AA57").ClearContents
Worksheets("Salary").Columns("AA").Hidden = True
Else: Worksheets("Salary").Columns("AA").Hidden = False
End If
If Worksheets("Settings").Range("D22").Value = "r" Then
Worksheets("Salary").Range("AB4:AB57").ClearContents
Worksheets("Salary").Columns("AB").Hidden = True
Else: Worksheets("Salary").Columns("AB").Hidden = False
End If
If Worksheets("Settings").Range("D23").Value = "r" Then
Worksheets("Salary").Range("AC4:AC57").ClearContents
Worksheets("Salary").Columns("AC").Hidden = True
Else: Worksheets("Salary").Columns("AC").Hidden = False
End If
If Worksheets("Settings").Range("D24").Value = "r" Then
Worksheets("Salary").Range("AD4:AD57").ClearContents
Worksheets("Salary").Columns("AD").Hidden = True
Else: Worksheets("Salary").Columns("AD").Hidden = False
End If
If Worksheets("Settings").Range("D25").Value = "r" Then
Worksheets("Salary").Range("AE4:AE57").ClearContents
Worksheets("Salary").Columns("AE").Hidden = True
Else: Worksheets("Salary").Columns("AE").Hidden = False
End If
If Worksheets("Settings").Range("D26").Value = "r" Then
Worksheets("Salary").Range("AF4:AF57").ClearContents
Worksheets("Salary").Columns("AF").Hidden = True
Else: Worksheets("Salary").Columns("AF").Hidden = False
End If
If Worksheets("Settings").Range("D27").Value = "r" Then
Worksheets("Salary").Range("AG4:AG57").ClearContents
Worksheets("Salary").Columns("AG").Hidden = True
Else: Worksheets("Salary").Columns("AG").Hidden = False
End If
If Worksheets("Settings").Range("D28").Value = "r" Then
Worksheets("Salary").Range("AH4:AH57").ClearContents
Worksheets("Salary").Columns("AH").Hidden = True
Else: Worksheets("Salary").Columns("AH").Hidden = False
End If
If Worksheets("Settings").Range("D29").Value = "r" Then
Worksheets("Salary").Range("AI4:AI57").ClearContents
Worksheets("Salary").Columns("AI").Hidden = True
Else: Worksheets("Salary").Columns("AI").Hidden = False
End If
If Worksheets("Settings").Range("D30").Value = "r" Then
Worksheets("Salary").Range("AJ4:AJ57").ClearContents
Worksheets("Salary").Columns("AJ").Hidden = True
Else: Worksheets("Salary").Columns("AJ").Hidden = False
End If
If Worksheets("Settings").Range("D31").Value = "r" Then
Worksheets("Salary").Range("AK4:AK57").ClearContents
Worksheets("Salary").Columns("AK").Hidden = True
Else: Worksheets("Salary").Columns("AK").Hidden = False
End If
If Worksheets("Settings").Range("D32").Value = "r" Then
Worksheets("Salary").Range("AL4:AL57").ClearContents
Worksheets("Salary").Columns("AL").Hidden = True
Else: Worksheets("Salary").Columns("AL").Hidden = False
End If
If Worksheets("Settings").Range("D33").Value = "r" Then
Worksheets("Salary").Range("AM4:AM57").ClearContents
Worksheets("Salary").Columns("AM").Hidden = True
Else: Worksheets("Salary").Columns("AM").Hidden = False
End If
End Sub