VBA to unlock worksheet

hayleyzim

New Member
Joined
Jul 7, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello all! I'm trying to finish a spreadsheet setup that someone else had started, and I'm not sure how to fix this error occurring in their VBA.

The function is used to unlock a specific worksheet for editing, but only certain users are allowed access to this. We have an authority tab where we have listed which users are allowed to edit the worksheet, which the VBA has been setup to search. If the user is on the authority list, a message box pops up saying "Successfully unlocked Course List.". If they're not on the list, it should pop up a message box saying "You are not authorised to edit the Course List tab.".

This function is working perfectly if the person it authorised to edit, however when someone without authority tries to edit it comes up with an error.

VBA Code:
Sub Unlock_CourseList()
Dim response As String
Dim current_user As String
Dim match_search As Variant

    current_user = Environ("Username")
    match_search = Application.Match(current_user, Range("CourseListAuthorities"), 0)
    If match_search = "" Then
      MsgBox ("You are not authorised to edit the Course List tab.")
    Else
      Worksheets("Course List").Unprotect Password:=PW_CourseList
      MsgBox ("Successfully unlocked Course List.")
    End If
        
End Sub

It is highlighting this particular part as the error:
VBA Code:
If match_search = "" Then

Can anyone please explain how I can fix this, so that if a person is not on the authority list then the message box will say "You are not authorised to edit the Course List tab."?
 

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.
Try replacing your code with this:
VBA Code:
Sub Unlock_CourseList()
Dim response As String
Dim current_user As String
Dim match_search As Variant
current_user = Environ("Username")
On Error Resume Next
match_search = Application.Match(current_user, Range("CourseListAuthorities"), 0)
    If IsError(match_search) Then
        On Error GoTo 0
        MsgBox ("You are not authorised to edit the Course List tab.")
    Else
      Worksheets("Course List").Unprotect Password:="PW_CourseList"
      MsgBox ("Successfully unlocked Course List.")
    End If
End Sub
 
Upvote 0
Solution
Try replacing your code with this:
VBA Code:
Sub Unlock_CourseList()
Dim response As String
Dim current_user As String
Dim match_search As Variant
current_user = Environ("Username")
On Error Resume Next
match_search = Application.Match(current_user, Range("CourseListAuthorities"), 0)
    If IsError(match_search) Then
        On Error GoTo 0
        MsgBox ("You are not authorised to edit the Course List tab.")
    Else
      Worksheets("Course List").Unprotect Password:="PW_CourseList"
      MsgBox ("Successfully unlocked Course List.")
    End If
End Sub
That fixed it, thank you so much for your help!
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,674
Members
448,977
Latest member
moonlight6

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