Saturday, July 25, 2009

Hardware Locking for excel workbook with VBA

Many time we desire that our workbook is locked to work with a particular machine. This can be achieved through Hardware Locking of your workbook using VBA. Some of the avaiable solutions will require you to buy external harware dongles and will cost you good amount of money.

However if you can lock your workbook with the existing hardware of the machine , you will not need external hardware dongles.

if you use following Excel function to get Mother Board Serial Number, you can compare the current machines mother board serial number with the desired one. If you know that the desired serial number does not match with current machine you know that there is security voilation and you can close the workbook through VBA. You will need to copy this code to regular VBA module of your workbook

Excel Function to Get Mother Board Serial Number

Public Function MBSerialNumber() As String
Dim objs As Object
Dim obj As Object
Dim WMI As Object
Dim sAns As String
Set WMI = GetObject("WinMgmts:")
Set objs = WMI.InstancesOf("Win32_BaseBoard")
For Each obj In objs
sAns = sAns & obj.SerialNumber
If sAns < objs.Count Then sAns = sAns & ","
MBSerialNumber = sAns

End Function

After you have placed above function in VBA module. You will need to copy this code to ThisWorkbook section of your workbook . This is autoopen macro which compares the MBSerialNumber of current machine with the desired MBSerialNumber. In case it does not mactch, it will close activeworkbook.
Private Sub Workbook_Open()
Set RMBSN = Sheets(1).Range("C4") ' This is whare you have already stored required MBSerialNumber
If MBSerialNumber <> RMBSN Then ' Checking if current machine serial number is matching with required
MsgBox ("Data Security failier, This workbook will close") ' In case it does not match workbook will be closed
End If
End Sub

Download Excel file with macro for Hardware Locking for Excel Workbooks

and try to open it on your machine and see how it works. This is locked version and you will not be able to open it on your machine.

However you can down load Unlocked version of Hardware Locking for Excel Workbooks.


  1. Be aware that sometimes this returns nothing, sometimes a single character - I have learnt this thinking I could use it a hardware locked serial number for my software.

  2. How to get my motherboard serial number and which line in your VB I can ammend the motherboard serial number? Your solution is very much appreciate. Thank you.

  3. You are the Man Yogesh. I have been using MAC address in one my Excel solution and it didnot work on few computers. Now, I have made this change to get a unique id from Mother Board. Hope this would work. Good useful info.

    1. Hey Yogesh. Richard has mentioned that this code returns only single character at times. Are you aware of any known issues that we need to take care? Would be helpful if you could share this info, so that everyone could take preventive measure in advance. :-)

  4. my pc serial number be 1234567890 (assumed) then what will be the code

    1. Step1.
      Unhide sheet1
      Change the serial number in c4.

    2. Step1.
      Unhide sheet1
      Change the serial number in c4.

  5. What could we use that would work both on a Mac and a PC? Since Excel is used on both, I am not sure if this solution would work on a Mac