Esta función la hice hace ya varios años, en un foro sobre temas de Excel una chica quería saber si existia alguna forma de obtener la IP de un equipo en Excel y fue así que hice esta solución.
El uso es muy sencillo, solo debes ingresar en cualquier celda =getMyIP(), una vez hecho te retornara la
información, ejemplo:
Copiar código
Function getMyIP() As String
Dim objWMI As Object
Dim objQuery As Object
Dim objQueryItem As Object
Dim vIpAddress As Variant
Dim res As String
On Error GoTo ErrorHandler
' Crear objeto WMI
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
' Consultar WMI
Set objQuery = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
res = ""
' Recorrer todas las direcciones IP asignadas
For Each objQueryItem In objQuery
For Each vIpAddress In objQueryItem.IPAddress
res = res & "User IP - " & vIpAddress & vbCrLf
Next
Next
getMyIP = Trim(res)
Cleanup:
' Limpiar objetos
On Error Resume Next
Set objQueryItem = Nothing
Set objQuery = Nothing
Set objWMI = Nothing
On Error GoTo 0
Exit Function
ErrorHandler:
' Manejo de errores
res = "Error: " & Err.Description
getMyIP = res
Resume Cleanup
End Function