I have a checkbox that does a lot of work on a worksheet depending on if its value is true or false. It was working fine until today, I decided that I wanted to have the checkbox also Lock or Unlock a cell dependent on its value.
The whole code is as follows:
The code highlighted in red is where I am running into an issue. For some reason, Excel is saying this is an invalid procedure or argument. Now, I am still an amateur when it comes to VBA excel, so I apologize if this is a simple fix, but I simply cannot figure this out. I have tried setting a worksheet unprotect and protect feature before and after the line of code, respectively, and still to no avail.
The whole code is as follows:
Code:
[INDENT=2]Private Sub CheckBox4_Click()[/INDENT]
[INDENT=2]Application.ScreenUpdating = False[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]Dim ws As Worksheet[/INDENT]
[INDENT=2]Dim sheetName As String[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2]If CheckBox4.Value = True Then[/INDENT]
[INDENT=2] ActiveWorkbook.Unprotect "Gravestrider89"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Unprotect "Gravestrider89"[/INDENT]
[INDENT=2] sheetName = Sheets("Control").Cells(16, "I")[/INDENT]
[INDENT=2] If sheetName = "" Then[/INDENT]
[INDENT=2] MsgBox "You must enter a valid Allowance descriptor. No entry was detected."[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] If Len(sheetName) > 31 Then[/INDENT]
[INDENT=2] MsgBox "Worksheet tab names cannot be greater than 31 characters in length."[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] Dim IllegalCharacter(1 To 7) As String, i As Integer[/INDENT]
[INDENT=2] IllegalCharacter(1) = "/"[/INDENT]
[INDENT=2] IllegalCharacter(2) = "\"[/INDENT]
[INDENT=2] IllegalCharacter(3) = "["[/INDENT]
[INDENT=2] IllegalCharacter(4) = "]"[/INDENT]
[INDENT=2] IllegalCharacter(5) = "*"[/INDENT]
[INDENT=2] IllegalCharacter(6) = "?"[/INDENT]
[INDENT=2] IllegalCharacter(7) = ":"[/INDENT]
[INDENT=2] For i = 1 To 7[/INDENT]
[INDENT=2] If InStr(sheetName, (IllegalCharacter(i))) > 0 Then[/INDENT]
[INDENT=2] MsgBox "You used a character that violates sheet naming rules. Please refrain from the following characters: / \ [ ] * ? : "[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] Next i[/INDENT]
[INDENT=2] If Sheets("Control").Range("I16") = Sheets("Control").Range("I17") Then[/INDENT]
[INDENT=2] MsgBox "There is already an Allowance with this name. Please choose a different name."[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] If Sheets("Control").Range("I16") = Sheets("Control").Range("I18") Then[/INDENT]
[INDENT=2] MsgBox "There is already an Allowance with this name. Please choose a different name."[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] If Sheets("Control").Range("I16") = Sheets("Control").Range("I21") Then[/INDENT]
[INDENT=2] MsgBox "There is already an Other Item with this name. Please choose a different name."[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] If Sheets("Control").Range("I16") = Sheets("Control").Range("I22") Then[/INDENT]
[INDENT=2] MsgBox "There is already an Other Item with this name. Please choose a different name."[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] If Sheets("Control").Range("I16") = Sheets("Control").Range("I23") Then[/INDENT]
[INDENT=2] MsgBox "There is already an Other Item with this name. Please choose a different name."[/INDENT]
[INDENT=2] Application.EnableEvents = False[/INDENT]
[INDENT=2] Sheets("Control").Cells(16, "I").ClearContents[/INDENT]
[INDENT=2] Application.EnableEvents = True[/INDENT]
[INDENT=2] CheckBox4.Value = False[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] If WorksheetExists(sheetName) Then[/INDENT]
[INDENT=2] Worksheets(sheetName).Visible = -1[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = False[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 2).Value = "ALL 1:"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 3).Value = "='Control'!I16"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 3).NumberFormat = "General"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 4).Value = "='Control'!K16"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 5).Value = "='Control'!L16"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 6).Value = "=" & sheetName & "!$H$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 7).Value = "=" & sheetName & "!$J$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 8).Value = "=" & sheetName & "!$N$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 9).Value = "=" & sheetName & "!$P$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 10).Value = "=SUM(F47:I47)/D47"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 11).Value = "=L47/F3"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 12).Value = "=" & sheetName & "!$U$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 13).Value = "=L47/$K$57"[/INDENT]
[INDENT=2] ActiveWorkbook.Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Sheets(sheetName).Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Sheets("SUMMARY").Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Exit Sub[/INDENT]
[INDENT=2] Else[/INDENT]
[INDENT=2] Set ws = Sheet34[/INDENT]
[INDENT=2] ws.Name = sheetName[/INDENT]
[INDENT=2] ws.Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] ws.EnableSelection = xlUnlockedCells[/INDENT]
[INDENT=2] Application.CutCopyMode = False[/INDENT]
[INDENT=2]
[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = False[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 2).Value = "ALL 1:"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 3).Value = "='Control'!I16"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 3).NumberFormat = "General"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 4).Value = "='Control'!K16"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 5).Value = "='Control'!L16"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 6).Value = "=" & ws.Name & "!$H$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 7).Value = "=" & ws.Name & "!$J$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 8).Value = "=" & ws.Name & "!$N$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 9).Value = "=" & ws.Name & "!$P$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 10).Value = "=SUM(F47:I47)/D47"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 11).Value = "=L47/F3"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 12).Value = "=" & ws.Name & "!$U$69"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Cells(47, 13).Value = "=L47/$K$57"[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] ActiveWorkbook.Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Sheets(sheetName).Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Sheets("SUMMARY").Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] Worksheets("Control").Activate[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] Worksheets("Control").Cells("I16").Locked = True[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2]End If[/INDENT]
[INDENT=2]If CheckBox4.Value = False Then[/INDENT]
[INDENT=2] ActiveWorkbook.Unprotect "Gravestrider89"[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Unprotect "Gravestrider89"[/INDENT]
[INDENT=2] sheetName = Sheets("Control").Cells(16, "I")[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] If WorksheetExists(sheetName) Then[/INDENT]
[INDENT=2] Worksheets(sheetName).Visible = 2[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Rows("47").EntireRow.ClearContents[/INDENT]
[INDENT=2] Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = True[/INDENT]
[INDENT=2] ActiveWorkbook.Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Sheets(sheetName).Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] Sheets("SUMMARY").Protect "Gravestrider89", True, True[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2][COLOR=#ff0000] Worksheets("Control").Cells("I16").Locked = False[/COLOR][/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2] End If[/INDENT]
[INDENT=2] [/INDENT]
[INDENT=2]End If[/INDENT]
[INDENT=2] Application.ScreenUpdating = True[/INDENT]
[INDENT=2]End Sub[/INDENT]