Good morning everybody! I've been using this piece of code to run a ping on a column of IP adresses and color the cells either green or red depending on the results. It works fine when I step through it using F8, however when it gets to an address that times out, it hangs the whole program for almost a full minute until it gets a result. Since this happens, when I try to run the script as intended, it hangs when it gets a timeout and does not recover. What I would like to do (or at least find out if it is possible or not) is a way to add a timer or adjust the timeout rate so it doesn't hang for so long. I will freely admit, I know very little about VBA scripting. Everything I know about VBA script, I have learned from trying to solve this problem. I've been searching all over for a solution to this and everything I find is either relatively complicated, or doesn't quite apply to what I'm trying to do. For reference, I am pinging printers on a network just to check connection status, and I would like to be able to send this spreadsheet to people and have them be able to run it without having to create additional files or anything too complicated. Is this possible or am I over-reaching on the capabilities of VBA?
Option Explicit
Private Declare Function GetRTTAndHopCount Lib "iphlpapi.dll" _
(ByVal iDestIPAddr As Long, _
ByRef iHopCount As Long, _
ByVal iMaxHops As Long, _
ByRef iRTT As Long) As Long
Private Declare Function inet_addr Lib "wsock32.dll" _
(ByVal cp As String) As Long
Sub TestPings()
Dim cell As Range
Dim astr() As String
For Each cell In Intersect(ActiveSheet.UsedRange, Columns(1))
astr = Split(cell.Value, ".")
If UBound(astr) = 3 Then
cell.Select
cell.Interior.ColorIndex = xlNone
cell.Interior.ColorIndex = IIf(Ping(cell.Text, 20), 4, 3)
End If
Next
End Sub
Public Function Ping(sIPadr As String, iMaxHops As Long) As Boolean
' Based on an article on CodeGuru by Bill Nolde
' Implemented in VBA in Nov 2002 by G. Wirth, Ulm, Germany
Const SUCCESS As Long = 1
Dim iIPadr As Long
Dim iHopCount As Long
Dim iRTT As Long
iIPadr = inet_addr(sIPadr)
Ping = (GetRTTAndHopCount(iIPadr, iHopCount, iMaxHops, iRTT) = SUCCESS)
Debug.Print "IP Address ....... " & iIPadr & vbLf _
& "HopCount ......... " & iHopCount & vbLf _
& "Round trip, ms ... " & iRTT
End Function
Bookmarks