Loading ...

How to Use a VBA to Find a Mac Address

Updated July 20, 2017

It's valuable to know how to check the Media Access Control (MAC) address using Visual Basic for Applications (VBA) when you're setting up multiple adaptors. Every adaptor has a permanent unique identification number known as a MAC address. The MAC address is used to identify the adaptor in a computer network. The number is 12 characters, with both numbers and letters, separated by dashes or colons. You can easily check the MAC address by creating a simple function using VBA.

Loading ...
  1. Open Microsoft Excel 2007 and type "Function GetMACAddress()" and press "Enter" to create a new function.

  2. Type "Dim objVMI As Object

  3. Dim vAdptr As Variant

  4. Dim objAdptr As Object

  5. Dim adptrCnt As Long" to create your variables.

  6. Type "Set objVMI = GetObject("winmgmts:\" & "." & "\root\cimv2")

  7. Set vAdptr = objVMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")" to define your adaptor objects.

  8. Type "For Each objAdptr In vAdptr

  9. Next" to loop through the network adaptors in your system and get your MAC address.

  10. Type "End Function" if necessary and run your function. You will see a message with your MAC address.

  11. Tip

    Your function should look like this: "Function GetMACAddress() Dim objVMI As Object Dim vAdptr As Variant Dim objAdptr As Object Dim adptrCnt As Long Set objVMI = GetObject("winmgmts:\" & "." & "\root\cimv2") Set vAdptr = objVMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") For Each objAdptr In vAdptr If Not IsNull(objAdptr.MACAddress) And IsArray(objAdptr.IPAddress) Then For adptrCnt = 0 To UBound(objAdptr.IPAddress) If Not objAdptr.IPAddress(adptrCnt) = "" Then GetNetworkConnectionMACAddress = objAdptr.MACAddress Exit For End If Next adptrCnt MsgBox "Your MAC Address is: " & GetNetworkConnectionMACAddress End If Next End Function"

Loading ...

About the Author

Jaime Avelar is a professional writer whose programming articles appear on various websites. He has been a software programmer since 2000. Avelar holds a Master of Science in information systems from the University of Texas at Arlington.

Loading ...