Ping function makes the whole excel table slow/unresponsive

前端 未结 2 842
攒了一身酷
攒了一身酷 2020-12-10 21:30

I have a function that pings computers from an excel list and gets the ping value of them.

While the script was running, the excel was completely unresponsive. I co

2条回答
  •  遥遥无期
    2020-12-10 22:16

    You might be able to implement something like this, but I haven't tried it with multiple servers

    • if your network is fast you can reduce the timeout to 500 ms or less:

    .

    Public Function serverOk(ByVal dbSrvrNameStr As String) As Boolean
    
        Const PINGS         As Byte = 1
        Const PING_TIME_OUT As Byte = 500
        Const PING_LOCATION As String = "C:\Windows\System32\"
    
        Dim commandResult As Long, serverIsActive As Boolean
    
        commandResult = 1
        serverIsActive = False
    
        If Len(dbSrvrNameStr) > 0 Then
    
            Err.Clear
    
            With CreateObject("WScript.Shell")
                commandResult = .Run("%comspec% /c " & PING_LOCATION & "ping.exe -n " & PINGS & " -w " & PING_TIME_OUT & " " & dbSrvrNameStr & " | find ""TTL="" > nul 2>&1", 0, True)
                commandResult = .Run("%comspec% " & PING_LOCATION & "/c ping.exe -n " & PINGS & " -w " & PING_TIME_OUT & " " & dbSrvrNameStr, 0, True)
                serverIsActive = (commandResult = 0)
            End With
    
            If serverIsActive And Err.Number = 0 Then
                '"DB Server - valid, Ping response: " & commandResult
            Else
                '"Cannot connect to DB Server, Error: " & Err.Description & ", Ping response: " & commandResult
            End If
            Err.Clear
        End If
    
        serverOk = serverIsActive
    End Function
    

    .

    Link to "Run Method (Windows Script Host)" from Microsoft:

    https://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx

    The 3rd parameter of this command can be overlooked: "bWaitOnReturn" - allows you to execute it asynchronously from VBA

提交回复
热议问题