Get mac address using excel vba

For security reasons we need to retrieve the mac address of system. MAC addresses are 12-digit hexadecimal numbers written in format MM:MM:MM:SS:SS:SS We can retrieve the mac address using various ways, but I am going to explain how do we get the mac address using excel vba.

Its not a very difficult task to get mac address using excel macro. Please copy and paste below code into your excel macro and it will automatically show you the mac address of your system.


Function GetMACAddress() As String
    Dim sComputer As String
    Dim oWMIService As Object
    Dim cItems As Object
    Dim oItem As Object
    Dim myMacAddress As String
    
    sComputer = "."
    
    Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
    
    Set cItems = oWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
        
    For Each oItem In cItems
        If Not IsNull(oItem.IPAddress) Then myMacAddress = oItem.macAddress
        Exit For
    Next
    'it will return mac address in format MM:MM:MM:SS:SS:SS
    MsgBox myMacAddress 
    GetMACAddress = myMacAddress

End Function

It will show you mac address into alert as well return string format when you call this function. Hope it will help you guys.

Cheers……

Posted by | View Post | View Group