Excel Interaction with Command Prompt

cbzdmh

Board Regular
Joined
Jul 16, 2007
Messages
58
Hi

I'm trying to get Excel to run a command from the command propmpt to see if a PC (named in cell A1 is pingable.

Is this possible?

If so how do i get the results back into Excel?

Many Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi

You can certainly use the Shell function to run the shell. I'm not sure about returning results to Excel though - I can imagine you could do it by outputting the results of the ping to a text file, and using VBA to open the text file and extract the contents.

Code:
Shell "ping www.google.com", vbNormalFocus
 
Upvote 0
You can ping using API functions - does your cell contain the IP address or the host name?
 
Upvote 0
I've kind of solved it, the code needs tidying up, but this works.


Sub ping()
Dim PC As String
Dim Pings, timeout As Integer
Dim Ping_Result As String
PC = "PCNAME"

Pings = 1
timeout = 100

Status = CreateObject("WScript.Shell"). _
Exec("%comspec% /c Ping -n " & Pings & " -w " & timeout & " " & PC).StdOut.ReadAll

If InStr(Status, "TTL=") = 0 Then
Ping_Result = " Doesn't Ping"
Else
Ping_Result = " Pings"
End If

MsgBox PC & Ping_Result

End Sub
 
Upvote 0
Instead of using the msgbox, (and assuming you have more than one computer to test), you can feed the return to the adjacent column. The only thing I'm uncertain about is whether you'd need to built a delayed timing loop between each run.

Sub ping()
Dim PC As String
Dim Pings, timeout As Integer
Dim Ping_Result As String

For looper = 1 To 1000
If Range("A" & looper).Value = "" Then Exit Sub

PC = Range("A" & looper).Value

Pings = 1
timeout = 100

Status = CreateObject("WScript.Shell"). _
Exec("%comspec% /c Ping -n " & Pings & " -w " & timeout & " " & PC).StdOut.ReadAll

If InStr(Status, "TTL=") = 0 Then
Ping_Result = " Doesn't Ping"
Else
Ping_Result = " Pings"
End If

Range("B" & looper) = PC & Ping_Result
Next looper
End Sub
 
Upvote 0
I just use msgbox as a way of checking I have an output I'm happy with before feeding the value to a cell.

Is there anyway of running all the commands using the same cmd window. Currently when i try running this for 20 machines, it opens (and shuts) 20 cmd windows. Or even hiding the CMD window?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top