DISCOVER
×

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.

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

Type "Dim objVMI As Object

Dim vAdptr As Variant

Dim objAdptr As Object

Dim adptrCnt As Long" to create your variables.

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

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

Type "For Each objAdptr In vAdptr

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

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

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) = "0.0.0.0" Then GetNetworkConnectionMACAddress = objAdptr.MACAddress Exit For End If Next adptrCnt MsgBox "Your MAC Address is: " & GetNetworkConnectionMACAddress End If Next End Function"

Cite this Article A tool to create a citation to reference this article Cite this Article

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.