Vba private scripts

I know that even private vba scripts can be called by the user, so making it "private" actually only hides his name.

However, is there a way to set up a macro so that it runs only if you are inside this particular VBA project? Not from Excel, not from VBScript or the like.

+5
source share
2 answers

If you want to block the code, you could

  • make code private so that macro names are not open
  • lock protection, and then verify that the project is really unlocked before allowing code to run

The sample code below verifies that the VBA in the host workbook is not protected before starting

Does this fit your needs?

    Private Sub TestMe()
    Dim objVB As Object
    Set objVB = ThisWorkbook.VBProject
    If objVB.Protection = 0 Then
        Call TestSub
    Else
        MsgBox "Sorry sport - unauthorised", vbCritical
    End If
    End Sub

    Private Sub TestSub()
    MsgBox "Project unprotected - i've been run", vbOK
    End Sub

enter image description here

+3
source

, . , , .

, VBA Code, , brettdj, - . . Excel Excel, ;)

Private , , , . , , Excel, , , .. VBA, .

, ( STOP - ), . FLAG. "", .

Option Explicit

Private Sub Sample(Auth As Boolean)
    If Auth = True Then
        '~~> Your macro code goes here
    End If
End Sub

, , VBA,

Sample True

Call Sample(True)

, .

VBS?

. . , ""

+1

All Articles