How to Use a VBA to Find a Mac Address

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use a VBA to Find a Mac Address
Use VBA to check your MAC address. (binary - o2 image by samantha grandy from Fotolia.com)

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.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

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

  2. 2

    Type "Dim objVMI As Object

    Dim vAdptr As Variant

    Dim objAdptr As Object

    Dim adptrCnt As Long" to create your variables.

  3. 3

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

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

  4. 4

    Type "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" to loop through the network adaptors in your system and get your MAC address.

  5. 5

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

Tips and warnings

  • 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"

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.