Running “Ping.exe” inside Excel through VBA

During this video tutorial, I will demonstrate how to run ping commands inside Excel. This can be used for many dos commands. This worksheet was originally written to do a nslookup on values; however, for this video it was quickly changed to run the ping command. The excel macro enabled workbook uses VBA, and is a very straight forward example of using Excel in non-traditional ways.

The workbook from the video (with the code) can be downloaded here.

About Constantine Krick

Once a teacher, now a word press blog
This entry was posted in Uncategorized. Bookmark the permalink.

19 Responses to Running “Ping.exe” inside Excel through VBA

  1. Tareq says:

    Hi
    This is very interesting post.
    I am using windows vista and I was trying to do something like this. I have downloaded your xls file and tried to run in my office 2007. But the Excel program crashes when i hit the ping button after puting IP 127.0.0.1
    I am novice in excel programming .. please help me
    thanks a lot

  2. Does it give you an error message? Try the ip 4.2.2.2 which is a global DNS server, and see if that crashes also. Let me know the results and any other clues.

  3. Oliver says:

    Hi
    This is very interesting post.
    I am using windows vista and I was trying to do something like this. I have downloaded your xls file and tried to run in my office 2007. But the Excel program crashes when i hit the ping button after puting IP 127.0.0.1
    I am novice in excel programming .. please help me
    thanks a lot

    +1

  4. Try 5 or fewer IP addresses to test. This example is only able to ping one address at a time. Until all the addresses have been pinged, the program will look like it has locked up since it can’t do anything until the vba code is finished (since it is all being ran in one thread). Being able to ping multiple addresses at the same time requires COM (Component Object Model) multi-threading, which is far beyond the scope of this tutorial. If you are interested in this, a great resource is the following discussion on MSDN : http://social.msdn.microsoft.com/forums/en-US/vsto/thread/735c8f26-2129-4b46-8c1a-aad385cab2ed/
    I would be curious if this fixes the issue.
    ~Constantine

  5. Mike Anning says:

    Hi Constantine
    Thank you for the video and code, works well and I learned something from it as I know nothing about VBA.
    Do you happen to have code that would post just the IP address returned into column B instead of the entire ping results?
    Thanks again
    Mike

  6. CVQ says:

    Hi Constantine,

    Thanks for this. Would you happen to have VBA code to ping email addresses to see if they are valid? If not, how would you recommend I approach this solution?

    Best,

    CVQ

  7. Looking up email addresses through this is not possible without major modifications. While I hate to say anything is impossible, I think there are much better solutions to use than this.

  8. D S Rathore says:

    Thanks for video and code, it’s working fine
    can you please suggest changes so that the result only show PASS of FAIL based on ping result in lieu of complete info.

  9. eloy says:

    thanks..is just what I looking for..only have to change value Rows, for Range to not delete the entire sheet.. (this is may first macro) thankyou very much!!

    example:

    Sub ResetValues()
    ‘ResetValues
    Range(“b6:b11”).Select
    Selection.ClearContents
    Rows(“6:6”).Select
    End Sub

  10. shudhanshu says:

    Nice program. I need Ping and Tracert report for same sheet can you help me with that?

  11. Without major modifications to the code, I would duplicate the first worksheet in Excel (Right click the worksheet, select “Move or Copy”, place a check in the “Create a copy” box, and press ok)… modify the code in the second sheet to do a Tracert, and then copy and paste the results into a third worksheet so you can see them all on one page.

  12. Vibhor says:

    This works great. I’m actually using ‘nslookup’ to get IP addresses from host names. However, it prints the entire output. I’m new to this and would really appreciate if you could suggest how to extract only the ‘IP address’ from nslookup output. Thanks!

  13. I would suggest looking into “Regular Expressions” which unfortunately is out of the scope of this tutorial.
    Regular Expressions :
    Scroll down to the section for IP Addresses : http://www.regular-expressions.info/examples.html
    How to add VBA Regular Expressions to your Excel sheet : http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops
    Good Luck,
    ~Constantine

  14. Cluters says:

    Hi Constantine,

    Thanks for this tutorial, everything worked OK

  15. Beth says:

    Constantine,
    I’m so glad you took the time to post this. What an amazing help!
    IF you ever get around to coding a change that incorporates the above requested Pass/Fail result instead of the entire ping result, I’d be thrilled to incorporate that into our spreadsheet.

  16. aladin says:

    Hello,,
    I need to ping an IP Address executing cmd commad in Microsoft Visio
    Could you help me

  17. hassnain says:

    Hello Sir, i tried your code as same at it is , but whenever i press the ping button it doesn’t work and it doesnt give me anything, please help its urgent 🙁

  18. Travis says:

    Hello Constantine,

    Thank you so much for this video. All the code I found on other sites never got close to working but I got this working fine. I subscribed to your channel just for this one tutorial!

    Was wondering if you could tell me how I can modify this so that all it does is highlight a cell green if there is a ping response, or red if no response (I can add colors later, just need to know how to format it so it doesn’t return the actual ping responses and just displays if there is a response). I’ve gotten it to highlight based on the value of the cell after the ping response but was wondering if this is possible without getting all the text from the response. I just need to know if it responds or it doesn’t.

    I’m also trying to figure out how I can keep the data in the first column so that it always uses the same static list of IP addresses. I’m still researching and stumbling through this but if you have any really quick pointers, I’d appreciate it.

    Thank you!!!

  19. raul says:

    its amazing…thanks a lot for making it out for us. great going.

Leave a Reply

Your email address will not be published. Required fields are marked *