Run-time error '13': Type mismatch

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
VBA Code:
Sub week_num()

    Dim ws As Worksheets, ws1 as Worksheets
    Dim wb as Workbook

    Set wb = ThisWorkbook
    
    WeekNum = Application.WorksheetFunction.RoundUp(Day(Now()) / 7, 0)
    
        Select Case WeekNum

            Case 1
                Set ws = wb.Worksheets("WO Week 1")[B] 'First occurrence happens here[/B]
                ws.Unprotect
                Set ws1 = wb.Worksheets("Past Due Week 1")
                ws1.Unprotect

            Case 2
                Set ws = wb.Worksheets("Past Due Week 2")
                ws.Unprotect
                Set ws1 = wb.Worksheets("WO Week 2")
                ws1.Unprotect
                
                
            Case 3
                Set ws = wb.Worksheets("Past Due Week 3")
                ws.Unprotect
                Set ws1 = wb.Worksheets("WO Week 3")
                ws1.Unprotect
                
                
                
            Case 4
                Set ws = wb.Worksheets("Past Due Week 4")
                ws.Unprotect
                Set ws1 = wb.Worksheets("WO Week 4")
                ws1.Unprotect
                
                
                
            Case 5
                Set ws = wb.Worksheets("Past Due Week 5")
                ws.Unprotect
                Set ws1 = wb.Worksheets("WO Week 5")
                ws1.Unprotect
                        
            Case Else
                ws.Protect
                ws1.Protect
                
        End Select
        
    Exit Sub

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
WorkSheet not WorkSheets
Rich (BB code):
Dim ws As Worksheets, ws1 as Worksheets
 
Upvote 0
It is because it is an object (you can select multiple sheets at a time)
 
Upvote 0
One other potential issue is Case Else where you're calling wb1.Protect but you have not set ws1.
You can simplify your case statements to this.
VBA Code:
...

    Select Case WeekNum
        Case 1 To 5
            Set ws = wb.Worksheets("Past Due Week " & WeekNum)
            Set ws1 = wb.Worksheets("WO Week " & WeekNum)
            ws.Unprotect
            ws1.Unprotect
        Case Else
           ws.Protect
    End Select
...
 
Upvote 0
There is an average of 5 weeks per month. So I have to two worksheets that need to be filled in depending on that week. So for the first week of the month, which we are in now, I have worksheets WO Week 1 and Past Due Week 1 unprotected, so no one can accidentally insert their data on the wrong week (worksheet). So the other sheets are protected are protected to prevent this from happening. They still can be viewed later on but can have data entered into it. I hope that makes sense. Thank you.
 
Upvote 0
Have you tried the suggested edits and tested it?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,114
Members
453,021
Latest member
Justyna P

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