NamSandStorm
NamSandStorm

Reputation: 155

VBA: How would my code check whether I have unlocked it?

Is there a way for my code to check whether the code in the VBA editor was unlocked for editing? I want to create a global "admin mode" variable which would give me more control in a dev vs. a live environment.

Upvotes: 1

Views: 226

Answers (1)

richardtallent
richardtallent

Reputation: 35404

This will do the trick, and should work as a UDF (though it will be volatile). However, this requires that macros have programmatic access to VBProject, which is NOT enabled by default and must be enabled (PER USER) in the Trust Center options in Excel. Chick and egg, there's no way to programmatically set that trust center option.

Public Function IsDevMode() As Boolean
  IsDevMode = (ThisWorkbook.VBProject.Protection=0)
End Function

However, there's no event to trap to know when this value changes, so it will be difficult to use it to make the changes to your workbook to move in and out of "developer mode." A better option might be to add a button somewhere that toggles "developer mode" (hiding and showing things, unlocking/locking sheets, etc.), and in that toggle button's click method, use the code above to see if the code has been unlocked in this session. If so, proceed, and if not, ignore. That way, by unlocking your code, you basically grant yourself access to use said button.

Upvotes: 1

Related Questions