如何执行cmd行命令将excel数据作为参数传递?

时间:2016-01-26 16:04:42

标签: excel excel-vba cmd vba

我们在excel中有一个很大的用户ID列表,并且希望能够单击用户ID并让它执行以下命令并显示结果:net user /domain <user ID>

我认为call shell是正确的方法......我不熟悉使用VB和excel。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

试试这个子。该子程序适用于一般的DOS /批处理命令。

var doc = XDocument.Load("filename.xml");
string[] lines = doc.Descendants("body")
                    .Elements("group")
                    .Elements("trans-unit")
                    .Elements("source")
                    .Select(e => (string)e).ToArray();

假设Private Sub Worksheet_Change(ByVal Target As Range) If Not Application.Intersect(Target, Range("A:A")) Is Nothing Then 'Call Shell("CMD.EXE /c " & Target.Value) Call Shell("cmd.exe /c net user /domain " & Target.Value) End If End Sub 位于User Name's双击任何具有用户名的单元格,请按Column AENTER

这是100%工作的截图(供您澄清)。 enter image description here

答案 1 :(得分:0)

您可以将其挂钩到工作表的选择更改事件中,代码将位于工作表模块中。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 Then 'assuming your ID's are in column A
        Call Shell("cmd.exe" & "rest of your command")
    End If
End Sub