Link to home
Start Free TrialLog in
Avatar of donnatamta
donnatamta

asked on

How do I return Hostname in excel cell by pinging IP address (VBA)

I have a spreadsheet that has range of IP addresses in column A and hostnames in Column B. not all IP addresses are assigned, so some of the column B is empty. I want to be able to ping IP in column A and check if it's assigned. If it's assigned and spreadsheed doesn't have the hostname in it already, then I want it to be inserted automaticaly. I have a code that returns ping time and it works fine, but I don't know how to return hostname.
Function PingTime(strAddress) As Long
Dim objPing As Object, objStatus As Object
 
    PingTime = "-1"
    Set objPing = GetObject("WinMgmts:{impersonationLevel=impersonate}").ExecQuery _
                           ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strAddress & "' ")
    For Each objStatus In objPing
        If Not IsNull(objStatus.StatusCode) And objStatus.StatusCode = 0 Then
            PingTime = objStatus.Properties_("ResponseTime").Value
            Exit For
        End If
    Next
    Set objStatus = Nothing
    Set objPing = Nothing
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of donnatamta
donnatamta

ASKER

This function gives me IP address, which I already have. I need to get hostname from IP address. Thanks
Works just peachy for me...did you try it? When I put in an IP address the result is the domain like "www.ibm.com".

Kevin