Closed Thread
Results 1 to 6 of 6

Ping Host Name Column A IP address Result column B Response time Column c

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2011
    Location
    Philadelphia Pa
    MS-Off Ver
    Excel 2007
    Posts
    3

    Lightbulb Ping Host Name Column A IP address Result column B Response time Column c

    I am trying to create a spread sheet that is able to take the Host Name in the first column and ping it. Result being the IP address in the second column and response time in the third. Which in turn tells me the health of the network connection. I have applications that do this, but i would like this excel based. Dont know too much about vbs code.

    Thanks in advance!
    -Nick

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Ping Host Name Column A IP address Result column B Response time Column c

    Hello NickMac,

    Here is a macro that will read the URLs in column "A" of the active sheet. The IP address, the response time in milliseconds, and the status are returned in columns "B:D". The URLs are assumed to start in "A2" with headers in row 1. The URL should just be "www.server.com" and not include the protocol.

    Ping Macro
    'Written: April 07, 2011
    'Author:  Leith Ross
    'Summary: Reads the URLs on the ActiveSheet from column "A2" and returns the IP address,
    '         response time, and status in columns "B:D". Headers are assumed to be in row 1.
    '         This works code with Windows XP and later.
    
    Sub PingTest()
    
      Dim Cell As Range
      Dim colPings As Object, objPing As Object, strQuery As String
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Wks = ActiveSheet
        
        Set Rng = Wks.Range("A2")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
        
        For Each Cell In Rng
        
          'Define the WMI query
           strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & Cell & "'"
    
          'Run the WMI query
           Set colPings = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
    
          'Translate the query results to either True or False
           For Each objPing In colPings
             If Not objPing Is Nothing Then
                Cell.Offset(0, 1) = objPing.ProtocolAddress
                Cell.Offset(0, 2) = objPing.ResponseTime & " ms"
                Cell.Offset(0, 3) = GetPingStatus(objPing.StatusCode)
             End If
           Next objPing
         
         Next Cell
        
    End Sub
    
    Function GetPingStatus(ByVal StatusCode As Long)
    
      Dim Result As String
    
          Select Case StatusCode
             Case 0: Result = "OK"
             Case 11001: Result = "Buffer too small"
             Case 11002: Result = "Destination net unreachable"
             Case 11003: Result = "Destination host unreachable"
             Case 11004: Result = "Destination protocol unreachable"
             Case 11005: Result = "Destination port unreachable"
             Case 11006: Result = "No resources"
             Case 11007: Result = "Bad option"
             Case 11008: Result = "Hardware error"
             Case 11009: Result = "Packet too big"
             Case 11010: Result = "Request timed out"
             Case 11011: Result = "Bad request"
             Case 11012: Result = "Bad route"
             Case 11013: Result = "Time-To-Live (TTL) expired transit"
             Case 11014: Result = "Time-To-Live (TTL) expired reassembly"
             Case 11015: Result = "Parameter problem"
             Case 11016: Result = "Source quench"
             Case 11017: Result = "Option too big"
             Case 11018: Result = "Bad destination"
             Case 11032: Result = "Negotiating IPSEC"
             Case 11050: Result = "General failure"
             Case Else: Result = "Unknown host"
          End Select
          
       GetPingStatus = Result
    
    End Function
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-07-2011
    Location
    Philadelphia Pa
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Ping Host Name Column A IP address Result column B Response time Column c

    [SOLVED]

    This is great! Thanks for the quick response Leith!

  4. #4
    Registered User
    Join Date
    05-19-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Ping Host Name Column A IP address Result column B Response time Column c

    Hi,

    I'm unsure if I've done something wrong - however - I've created a new module, copied - pasted word for word the block of code above - my url list is in the required www.server.com format - however when i click run the ms appears in cell C2 but there is a Run-time error '94': Invalid use of null and when i select debug the below line is highlighted..

    Cell.Offset(0, 3) = GetPingStatus(objPing.StatusCode)

    in the block

    'Translate the query results to either True or False
    For Each objPing In colPings
    If Not objPing Is Nothing Then
    Cell.Offset(0, 1) = objPing.ProtocolAddress
    Cell.Offset(0, 2) = objPing.ResponseTime & " ms"
    Cell.Offset(0, 3) = GetPingStatus(objPing.StatusCode)
    End If
    Next objPing

    thoughts? thanks!

  5. #5
    Registered User
    Join Date
    04-07-2011
    Location
    Philadelphia Pa
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Ping Host Name Column A IP address Result column B Response time Column c

    Kinda on the same topic, maybe in reverse. How would i go about creating a subnet scanner? Would return items from DNS such as Host name, IP address, status based on a subnet. ex. 10.10.10.0 - 10.10.10.255.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Ping Host Name Column A IP address Result column B Response time Column c

    Nickmac,

    This is an old thread. Please create a new thread and provide a link to this one if required.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1