Excel 在背景中无声地ping
当我执行以下代码时,会打开一个黑色的命令窗口,它将闪烁,直到所有设备发出ping声。我怎样才能安静地运行它Excel 在背景中无声地ping,excel,vba,Excel,Vba,当我执行以下代码时,会打开一个黑色的命令窗口,它将闪烁,直到所有设备发出ping声。我怎样才能安静地运行它 Sub PING() Application.ScreenUpdating = False Dim strTarget, strPingResult, strInput, wshShell, wshExec With Sheets(1) shlastrow = .Cells(Rows.Count, "B").End(xlUp).Row Set shrange = .Ra
Sub PING()
Application.ScreenUpdating = False
Dim strTarget, strPingResult, strInput, wshShell, wshExec
With Sheets(1)
shlastrow = .Cells(Rows.Count, "B").End(xlUp).Row
Set shrange = .Range("B3:B7" & shlastrow)
End With
For Each shCell In shrange
strInput = shCell.Text
If strInput <> "" Then
strTarget = strInput
setwshshell = CreateObject("wscript.shell")
Set wshExec = wshShell.exec("ping -n 2 -w 5 " & strTarget)
strPingResult = LCase(wshExec.stdout.readall)
If InStr(strPingResult, "reply from") Then
shCell.Offset(0, 1).Value = "Reachable"
shCell.Offset(0, 2).Value = "Time"
Else
shCell.Offset(0, 1).Value = "UnReachable"
shCell.Offset(0, 2).Value = "Reachable"
End If
End If
Next shCell
End Sub
子PING()
Application.ScreenUpdating=False
Dim strTarget、strPingResult、strInput、wshShell、wshExec
附页(1)
shlastrow=.Cells(Rows.Count,“B”).End(xlUp).Row
设置shrange=.Range(“B3:B7”和shlastrow)
以
对于shrange中的每个shCell
strInput=shCell.Text
如果输入“”则
strTarget=strInput
setwshshell=CreateObject(“wscript.shell”)
设置wshExec=wshShell.exec(“ping-n2-w5”和strTarget)
strPingResult=LCase(wshExec.stdout.readall)
如果InStr(strpingreult,“回复自”)则
shCell.Offset(0,1).Value=“可到达”
shCell.Offset(0,2).Value=“时间”
其他的
shCell.Offset(0,1).Value=“无法访问”
shCell.Offset(0,2).Value=“可到达”
如果结束
如果结束
下一个shCell
端接头
这是代码
副杜平()
与ActiveWorkbook.Worksheets(1)
n=0
行=2
做
如果.Cells(第1行)为“”,则
如果不可连接(.Cells(Row,1),2100)=True,则
n=n+1
单元格(第1行).Interior.Color=RGB(0,255,0)
单元格(第1行)。Font.FontStyle=“bold”
单元格(第1行)。Font.Size=14
单元格(第2行).Interior.Color=RGB(0,255,0)
单元格(第2行)。值=时间
“鸣笛
其他:
n=n+1
'单元格(第2行)。公式=“=NOW()-”&CDbl(NOW())
单元格(第1行).Interior.Color=RGB(255,0,0)
单元格(第3行)。Value=DateDiff(“h:mm:ss”,单元格(第2行),Now())
如果结束
如果结束
行=行+1
循环直到。单元格(第1行)=“”
以
端接头
功能不可连接(sHost、iPings、iTO)
'根据ping.exe的输出返回True或False
'工作于“所有”WSH版本
'sHost是主机名或IP
'iPings是ping尝试次数
'iTO是以毫秒为单位的超时
'如果值设置为“”,则使用以下默认值
暗淡的
如果iPings=”“,则iPings=1'默认ping数
如果iTO=”“,则iTO=550'默认每次ping超时
使用CreateObject(“WScript.Shell”)
nRes=.Run(“%comspec%/c ping.exe-n”&iPings&“-w”&iTO)_
&“&sHost&”查找“TTL=”“>num2>&1”,0,真)
以
不可连接=(nRes=0)
端函数
尝试使用strpingreult=Shell(“ping-n2-w5”&strTarget,vbHide)
我可以在哪里插入它,或者我需要替换一些东西。对不起,先生,我是vba新手。替换您的设置wshExec=wshShell.exec(“ping-n2-w5”&strTarget)
以及strpingreult=LCase(wshExec.stdout.readall)
亲爱的Shai Rado可能存在重复,但它不工作
With ActiveWorkbook.Worksheets(1)
n = 0
Row = 2
Do
If .Cells(Row, 1) <> "" Then
If IsConnectible(.Cells(Row, 1), 2, 100) = True Then
n = n + 1
Cells(Row, 1).Interior.Color = RGB(0, 255, 0)
Cells(Row, 1).Font.FontStyle = "bold"
Cells(Row, 1).Font.Size = 14
Cells(Row, 2).Interior.Color = RGB(0, 255, 0)
Cells(Row, 2).Value = Time
'Call siren
Else:
n = n + 1
'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now())
Cells(Row, 1).Interior.Color = RGB(255, 0, 0)
Cells(Row, 3).Value = DateDiff("h:mm:ss", Cells(Row, 2), Now())
End If
End If
Row = Row + 1
Loop Until .Cells(Row, 1) = ""
End With
End Sub
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Dim nRes
If iPings = "" Then iPings = 1 ' default number of pings
If iTO = "" Then iTO = 550 ' default timeout per ping
With CreateObject("WScript.Shell")
nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & " | find ""TTL="" > nul 2>&1", 0, True)
End With
IsConnectible = (nRes = 0)
End Function