Skip to content

Protect an Excel Workbook

You’ve built a powerful Excel application with VBA macros, and now you want to distribute it without exposing your source code. In this tutorial, you’ll walk through the complete workflow: compiling your VBA logic into a protected DLL, wiring it up to Excel through the auto-generated VBA Bridge, and optionally adding activation keys with hardware locking.

What you’ll build:

  • A compiled VBA DLL that replaces your original macros
  • A VBA Bridge that lets your Excel workbook call the protected code
  • Caller macros in Excel that invoke your compiled functions
  • (Optional) An activation system with hardware-locked license keys

Time needed: 15-20 minutes


Start by opening your Excel file in VBA Padlock. This creates a project that will hold your compiled code, licensing settings, and build configuration.

  1. Launch VBA Padlock

  2. Click “Open Office File” in the ribbon (or press Ctrl+O)

  3. Select your Excel workbook or add-in (.xlsm or .xlam file) — for this tutorial, we’ll use a file called MyExcelApp.xlsm

  4. VBA Padlock analyzes your file and displays the Project Info panel

VBA Padlock Project Information settings

In the Project Info panel, fill in these fields:

FieldExample ValuePurpose
DLL NameMyExcelAppBase name for all output DLL files
Application TitleMy Excel ApplicationShown in activation dialogs and title bars
Version Number1.0.0.0Your application’s version
File DescriptionProtected Excel VBA MacrosAppears in Windows file properties

Now switch to the Code Editor in VBA Padlock. This is where you write the VBA code that will be compiled into the DLL. The code you write here will no longer be visible as plain-text VBA — it runs inside the protected DLL.

Here is an example script that demonstrates the key patterns — subs, functions with parameters, reading/writing cells, and interacting with the workbook:

Sub HelloWorld()
On Error Resume Next
MsgBox "Hello from VBA Padlock!", vbInformation, "MyExcelApp"
End Sub
Function Greet(UserName)
If Len(Trim(UserName)) = 0 Then
Greet = "Hello, Anonymous!"
Else
Greet = "Hello, " & Trim(UserName) & "!"
End If
End Function
Sub ShowWorkbookInfo()
Dim Info, WB
Set WB = Application.ActiveWorkbook
Info = "Workbook Information" & Chr(13) & Chr(13)
Info = Info & "Name: " & WB.Name & Chr(13)
Info = Info & "Path: " & WB.Path & Chr(13)
Info = Info & "Sheets: " & WB.Sheets.Count
MsgBox Info, vbInformation, "Workbook Info"
End Sub
Sub WriteToCell(CellAddress, Value)
Application.ActiveSheet.Range(CellAddress).Value = Value
End Sub
Function ReadFromCell(CellAddress)
ReadFromCell = Application.ActiveSheet.Range(CellAddress).Value
End Function
Function CalculateDiscount(Price, DiscountRate)
If DiscountRate < 0 Or DiscountRate > 1 Then
CalculateDiscount = Price
Else
CalculateDiscount = Price * (1 - DiscountRate)
End If
End Function

Writing protected VBA code in the internal editor

Type or paste this code into the VBA Padlock Code Editor. You can adapt it to your own application — the important thing is that each Sub or Function you define here becomes callable from Excel through VBAPL_Execute.


Time to build the protected DLL.

  1. Click “Publish Final DLL” in the ribbon (Project and Build tab)

  2. Click “Build Final DLL Files” in the Publish dialog

  3. VBA Padlock will:

    • Parse your VBA code
    • Compile it into protected bytecode
    • Embed it into a protected DLL
    • Generate the runtime DLLs
  4. When the build completes, you’ll see a success message. Three DLL files are created in the bin\ subfolder next to your Excel workbook:

MyExcelApp\
├── MyExcelApp.xlsm
└── bin\
├── MyExcelApprun32.dll (32-bit runtime)
├── MyExcelApprun64.dll (64-bit runtime)
└── MyExcelApp.dll (your compiled code - satellite DLL)

The VBA Bridge is a standard VBA module that VBA Padlock generates and injects into your Excel workbook. It contains all the Declare statements and wrapper functions (VBAPL_Execute, VBAPL_IsLicenseValid, etc.) that let your Excel VBA code communicate with the compiled DLL.

You do not write the bridge code manually — VBA Padlock creates it for you.

  1. Open your MyExcelApp.xlsm file in Excel (enable macros when prompted)

  2. Go back to VBA Padlock and click “Create VBA Bridge” in the ribbon

  3. Click “Inject Into Office”

  4. VBA Padlock inserts a new module named VBADLLBridge (or similar) into your workbook’s VBA project. This module contains all the VBAPL_* functions.

The VBA Bridge module imported into the Excel VBA project

Open the VBA Editor (Alt+F11) in Excel and you’ll see the new bridge module. It includes functions such as:

  • VBAPL_Execute — call any Sub or Function compiled in the DLL
  • VBAPL_IsLicenseValid — check whether the application is activated
  • VBAPL_ShowActivation — display the activation dialog
  • VBAPL_GetHardwareID — retrieve the user’s hardware identifier
  • VBAPL_IsTrialMode — check if running in trial mode
  • And more (see the VBA Bridge API Reference for the complete list)

Now that the VBA Bridge is in place, you can call your compiled functions from regular Excel VBA code. The key function is VBAPL_Execute — you pass it the name of a Sub or Function (as a string), followed by any arguments.

In the VBA Editor, insert a new standard module (for example, Module1) and add your caller macros:

Sub Demo_HelloWorld()
Call VBAPL_Execute("HelloWorld")
End Sub
Sub Demo_Greet()
Dim UserName As String, result As Variant
UserName = InputBox("Enter your name:", "Greet Demo")
If Len(UserName) > 0 Then
result = VBAPL_Execute("Greet", UserName)
MsgBox result, vbInformation, "Greeting"
End If
End Sub
Sub Demo_WorkbookInfo()
Call VBAPL_Execute("ShowWorkbookInfo")
End Sub
Sub Demo_WriteToCell()
Call VBAPL_Execute("WriteToCell", "A1", "Hello from DLL!")
MsgBox "Value written to A1", vbInformation
End Sub
Sub Demo_ReadFromCell()
Dim Value As Variant
Value = VBAPL_Execute("ReadFromCell", "A1")
MsgBox "Value in A1: " & CStr(Value), vbInformation
End Sub
Sub Demo_CalculateDiscount()
Dim Price As Double, DiscountRate As Double, FinalPrice As Variant
Price = 100: DiscountRate = 0.15
FinalPrice = VBAPL_Execute("CalculateDiscount", Price, DiscountRate)
MsgBox "Original: $" & Format(Price, "0.00") & vbCrLf & _
"Discount: " & Format(DiscountRate * 100, "0") & "%" & vbCrLf & _
"Final: $" & Format(FinalPrice, "0.00"), vbInformation, "Discount Calculator"
End Sub

Notice the pattern:

  • For Subs (no return value): Call VBAPL_Execute("SubName", arg1, arg2, ...)
  • For Functions (returns a value): result = VBAPL_Execute("FunctionName", arg1, arg2, ...)

The first argument is always the name of the compiled Sub or Function as a string. Any additional arguments are passed through to that Sub or Function.

Save the workbook.


Let’s verify everything works end to end.

  1. Close and reopen MyExcelApp.xlsm in Excel

  2. Enable macros when prompted (click “Enable Content”)

  3. Press Alt+F8 to open the Macro dialog

  4. Select Demo_CalculateDiscount and click Run

  5. You should see a message box displaying the calculation result:

Excel execution result

Calling the protected DLL function from the Excel VBA editor

Excel worksheet displaying the result of the protected custom function

If all the demos work, your code is successfully compiled into the DLL and running through the VBA Bridge.


If you want to require an activation key before your application runs, follow this step. If you’re just protecting your code without licensing, you can skip ahead to Step 8.

  1. Switch to the “Licensing Features” tab in the VBA Padlock ribbon

  2. Click “Activation Settings”

  3. Check “Activation key is required to run the protected VBA application”

  4. Click OK

VBA Padlock Activation Settings and licensing options

Hardware locking ties each license key to a specific computer, preventing key sharing.

  1. In the Activation Settings dialog, check “Create hardware-locked keys”

  2. Click “Hardware ID Options…” to choose which hardware components to include:

    • Computer Name (recommended)
    • Windows Volume Serial (recommended)
    • BIOS Serial Number (recommended)
    • CPU Serial (optional — some CPUs don’t report it)
    • MAC Address (not recommended — changes with network adapters)
  3. Click OK

After changing licensing settings, you need to rebuild the DLL:

  1. Go back to the “Project and Build” tab

  2. Click “Publish Final DLL” then “Build Final DLL Files”

  3. The new DLL now enforces activation

To test the activation flow on your own computer:

  1. Click “Key Generator” in the Licensing Features ribbon

  2. The Hardware ID field shows your computer’s ID

  3. Click “Generate Key”

  4. Copy the generated key — you’ll enter it when testing

VBA Padlock Activation Key Generator dialog

Update the Workbook_Open event in ThisWorkbook:

Private Sub Workbook_Open()
' Check if the application is activated
If Not VBAPL_IsLicenseValid() Then
VBAPL_ShowActivation
End If
' After activation, the user can use the application normally
End Sub

You can also let users retrieve their Hardware ID so they can send it to you:

Sub ShowMyHardwareID()
Dim hwid As String
hwid = VBAPL_GetHardwareID()
MsgBox "Your Hardware ID is:" & vbCrLf & hwid, vbInformation, "Hardware ID"
End Sub

Rebuild the DLL one more time after making these changes, then test by opening the workbook — you should see the activation dialog.


You’re ready to ship. VBA Padlock includes a Distribution dialog to package your files with the correct folder structure.

  1. Click “Distribute” in the ribbon (Project and Build tab)

  2. Choose one of two options:

    • Create ZIP archive — creates a ready-to-ship ZIP file
    • Copy to folder — copies all files to a specified directory
  3. For this tutorial, select “Create ZIP archive”

  4. Choose a destination and filename (e.g., MyExcelApp_v1.0.zip)

  5. VBA Padlock creates a ZIP containing:

    MyExcelApp_v1.0.zip
    ├── MyExcelApp.xlsm
    └── bin\
    ├── MyExcelApprun32.dll
    ├── MyExcelApprun64.dll
    └── MyExcelApp.dll

Include this in your distribution (e.g., in a README.txt):

INSTALLATION INSTRUCTIONS
=========================
1. Extract all files to a folder on your computer (e.g., C:\MyExcelApp\)
2. IMPORTANT: Keep the folder structure intact. The 'bin' folder must remain
in the same directory as MyExcelApp.xlsm
3. Open MyExcelApp.xlsm in Excel
4. If you see a security warning, click "Enable Content"
5. If prompted, enter your activation key and click "Activate"
6. You're ready to use the application!
IMPORTANT:
- Keep MyExcelApp.xlsm and the 'bin' folder together at all times
- Do not move or rename files in the 'bin' folder
- The 'bin' folder contains 3 DLL files that are required for operation

Congratulations! You’ve successfully compiled your Excel VBA code into a protected DLL and wired it up through the VBA Bridge. Your source code is no longer visible in the workbook.

Online Activation

Automate activation with a PHP server so users can activate over the internet.

Read the guide

Trial Mode

Let users try your application before requiring a license key.

Learn more

License Deactivation

Allow users to transfer their license to a new computer.

Learn more

Advanced Features

Explore EULA display, localization, and code obfuscation.

Browse features


”Run-time error ‘48’: File not found”

Section titled “”Run-time error ‘48’: File not found””

Cause: Excel cannot find the DLL files in the bin\ folder.

Solution:

  1. Make sure the bin\ folder exists in the same directory as MyExcelApp.xlsm
  2. Verify that the bin\ folder contains all three DLL files:
    • MyExcelApprun32.dll
    • MyExcelApprun64.dll
    • MyExcelApp.dll
  3. Do not rename or move the bin\ folder or any files inside it

Cause: The activation key does not match the user’s Hardware ID.

Solution:

  1. Ask the user for their Hardware ID (they can run the ShowMyHardwareID macro or call VBAPL_GetHardwareID())
  2. Generate a new key specifically for that Hardware ID using the Key Generator
  3. Do not reuse keys generated for a different computer

Cause: The stored license was deleted (e.g., Windows reinstall, registry cleanup).

Solution: The user needs to reactivate with their existing key. If the hardware has not changed, the same key will work again.

Cause: Excel’s security settings block macros.

Solution: Add the application folder to Excel’s Trusted Locations:

  1. Go to File > Options > Trust Center > Trust Center Settings
  2. Click Trusted Locations
  3. Click Add new location and browse to your application folder

”Type mismatch” or unexpected return value

Section titled “”Type mismatch” or unexpected return value”

Cause: The return value from VBAPL_Execute is a Variant. If you assign it to a strongly typed variable without conversion, you may get a type mismatch.

Solution: Use CStr(), CDbl(), CLng(), or similar conversion functions when assigning the result to a typed variable, or declare your receiving variable as Variant.