Ping IP Address with VBA code and return results in Excel

前端 未结 2 1200
名媛妹妹
名媛妹妹 2020-12-01 03:18

I have some visual basic code (see below) that tests an IP connection in column B (of an excel spreadsheet) and puts whether or not it is connected or un-reachable in column

相关标签:
2条回答
  • 2020-12-01 03:35

    You don't need code for this. Turn all the cells red, then add conditional formatting to make it green when you want.

    Home > Conditional Formatting > New Rule > Use a formula...

    =C2="Connected"
    

    and format to green. If you want to do it in code, you can add some lines in your For Each loop

    If Result = "Connected" Then
        Cell.Offset(0,1).Font.Color = vbGreen
    Else
        Cell.Offset(0,1).Font.Color = vbRed
    End If
    
    0 讨论(0)
  • 2020-12-01 03:51

    To have this run automatically at certain intervals, check out this link.

    Here's the relevant code:

    Public dTime As Date
    Dim lNum As Long
    
    Sub RunOnTime()
        dTime = Now + TimeSerial(0, 0, 10) 'Change this to set your interval
        Application.OnTime dTime, "RunOnTime"
    
        lNum = lNum + 1
        If lNum = 3 Then
            Run "CancelOnTime" 'You could probably omit an end time, but I think the program would eventually crash
        Else
            MsgBox lNum
        End If
    
    End Sub
    
    Sub CancelOnTime()
        Application.OnTime dTime, "RunOnTime", , False
    End Sub
    

    I would recommend including a ThisWorkbook.Save line as I can't speak to how long this will run without crashing, and I would imagine you could see problems if you left it for days at a time.

    0 讨论(0)
提交回复
热议问题