LoginSignup
0
0

【vba】お手軽だけど実践的なpingの使い方を公開してみた!

Last updated at Posted at 2023-09-19

クラスCの全てのIPアドレス(192.168.255.1~192.168.255.255)にpingをし、どの端末が接続しているか確認しました。
p1.png

確認したいネットワークがクラスCって所まで分かっているけど、
どの端末が接続しているか分からない場合、皆さんならどうしますか?

承転

私の場合コードで解決する事にしました。

Sub main()
Call stop_display
Call try_ping
Call start_display
End Sub
Sub try_ping()
    ' セルからIPアドレスを順に取得してping送信
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Application.StatusBar = "*** 処理中...(" & i & "/255) ***"    
        Dim wsh As Object: Set wsh = CreateObject("Wscript.Shell")
        Dim host As String: host = Cells(i, 1).Value
        
        Const sendCount As Long = 1 '実施回数
        Const timeOut As Long = 100 'タイムアウトまでの秒数
         
        Dim cmd As String: cmd = "ping -n " & sendCount & " -w " & timeOut & " " & host
        Call_CheckPing = wsh.Run(cmd, 7, True) = 0
            
        'Pingが届いたらTrue/届かなければFalse
        If Call_CheckPing = False Then
            Cells(i, 2).Value = "失敗"
        Else
            Cells(i, 2).Value = "成功"
        End If
    Next
    Application.StatusBar = False
End Sub
Sub stop_display()
    Application.ScreenUpdating = False '画面更新をOFF
    Application.Calculation = xlManual '自動計算をOFF
    Application.EnableEvents = False 'イベントをOFF
End Sub
Sub start_display()
    Application.ScreenUpdating = True '画面更新をON
    Application.Calculation = xlAutomatic '自動計算をON
    Application.EnableEvents = True 'イベントをON
End Sub

今回のポイントは以下の3点です。

  • 画面の更新を停止させ、処理を高速化している点
  • ステータスバーに現在何行目を処理しているか表示させている点
  • 条件書式で「成功」が見やすい点

備考(条件書式)

B列選択後、
ホーム→条件書式→セル強調→文字列
p2.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0