Find Local IP Address – VBA Macro Code

Computer IP Address – Local & Public

IP stand for Internet Protocol address. It is assigned to every network device – be in a computer, smartphone, router etc.,

This address is used to send the Internet data packets to the correct device attached to internet.

The IP address have different version – IPv4 (32 bit address) & IPv6 (128 Bits).

Also, a computer can have a Local IP Address & Public IP Address when it is attached to a network router or switch.

VBA Code to Get Local IP Address

This macro code will fetch the local ip addresses assigned to your computer.

To get this detail, we will be querying the WMI (Windows Management Instrumentation)

Public Function getMyIP()
    'Related Reference - Microsoft WMI Scripting V1.2 Library (wbemdisp.TLB)
    'Define Variable Data Types
    Dim objWMI As Object
    Dim objQuery As Object
    Dim objQueryItem As Object
    Dim vIpAddress
    
    'Create WMI Object
    Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
    
    'Query WMI
    Set objQuery = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    
    'Loop Thru all assigned IP Addresses
    For Each objQueryItem In objQuery
        Debug.Print vbCrLf & "All Assigned IP Addresses"
        For Each vIpAddress In objQueryItem.ipaddress
            Debug.Print "User IP - " & vIpAddress
        Next
    Next
    
End Function

Press F5 to run the code. You can view the results in the immediate window (Ctrl + G).

This will output all the IP addresses assigned for the computer. There can be more than one IP address for a computer depending on the number of Network adapters it is attached to. For example: One compute can have a Ethernet, Wifi, Bluetooth adapter.

WMI can be used to get many more details about the Computer devices. Click here to know more about it.

Note: WMIC is deprecated & replaced with PowerShell cmdlets.