excel - 无法更改 Excel VBA IP 列表 ping 中的 ping 超时

标签 excel ping vba

以下代码对 Excel 工作表中的 IP 地址列表执行 ping 操作,并返回响应时间和 TTL。根据 IP 地址的数量,超时可能会很快累积起来并导致长时间等待。有没有办法添加 5​​00 毫秒的自定义超时?

Sub Ping_Check()
' Based on http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/e59a38e1-eaf0-4b13-af10-fd4be559f50f/
Dim oPing As Object
Dim oRetStatus As Object
Dim xCell As Range
Dim xLast_Row As Long
Dim xWork1 As String

xLast_Row = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

Application.ScreenUpdating = False

    For Each xCell In Range("A2:A" & xLast_Row)
        If xCell = "" Then
            xCell.Offset(0, 1) = ""
        Else
            Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & xCell & "'")
            For Each oRetStatus In oPing
                If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
                    xCell.Offset(0, 1) = "N/A"
                    '11001   Buffer Too Small
                    '11002   Destination Net Unreachable
                    '11003   Destination Host Unreachable
                    '11004   Destination Protocol Unreachable
                    '11005   Destination Port Unreachable
                    '11006   No Resources
                    '11007   Bad Option
                    '11008   Hardware Error
                    '11009   Packet Too Big
                    '11010   Request Timed Out
                    '11011   Bad Request
                    '11012   Bad Route
                    '11013   TimeToLive Expired Transit
                    '11014   TimeToLive Expired Reassembly
                    '11015   Parameter Problem
                    '11016   Source Quench
                    '11017   Option Too Big
                    '11018   Bad Destination
                    '11032   Negotiating IPSEC
                    '11050   General Failure
                Else
                    xCell.Offset(0, 1) = oRetStatus.ResponseTime & " ms ; " & oRetStatus.ResponseTimeToLive
                End If
            Next
        End If
    Next

Application.ScreenUpdating = True

End Sub

最佳答案

根据MSDN page on Win32_PingStatus有一个名为“超时”(以毫秒为单位)的属性可能会被更改。

尝试将您的查询更改为

"select * from Win32_PingStatus where TimeOut = 500 and address = '" & xCell & "'"

看起来默认是1000毫秒

关于excel - 无法更改 Excel VBA IP 列表 ping 中的 ping 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34682073/

相关文章:

excel - 如何将 Access 数据库中的所有表导出到 Excel - 每个表的工作表

excel - Excel VBA application.worksheetfunction 的简写

vba - 日文键盘编程 "Henkan"「変换」按钮

linux - 我想从命令输出到 shell expect 中的变量

arrays - 如何在 VBA 中将二维数组的一维分配给新的一维数组

excel - 仅选择包含两列之间数据的单元格

mysql - Galera 集群高 Ping

linux - grep ping 输出的持续时间

excel - 可变数量的嵌套循环

arrays - 删除数组中的目录