[英]VBA - Send command which in cells to CMD to run
Here is my situation: we need to create a folder and grant the folder permission for new staff everyday and i am creating the excel with VBA Macros. 这是我的情况:我们需要每天创建一个文件夹并向新员工授予该文件夹的权限,而我正在使用VBA宏创建excel。 I want to make it like that, after input the user ID, the command (use it in CMD) will generated in other cell (eg D6 - D10, depend how many new staff on that day), and when i press the button, the folder with created with the name of the user id. 我想这样,输入用户ID后,该命令(在CMD中使用它)将在其他单元格中生成(例如D6-D10,取决于当天有多少新员工),当我按下按钮时,使用用户ID名称创建的文件夹。 And i want to add a msgbox saying the folder has been created with no error. 我想添加一个msgbox,表示该文件夹已创建,没有错误。
Here is the excel: https://i.stack.imgur.com/bBDPZ.png 这是excel: https : //i.stack.imgur.com/bBDPZ.png
what i want to do is to make a button, press it and the folder created, i have tried for 2 days and got many outcome 我想要做的是制作一个按钮,按下它并创建文件夹,我尝试了2天,得到了很多结果
here is my code, the range in excel is D6 to D55, here only what i test: 这是我的代码,Excel中的范围是D6到D55,这只是我测试的内容:
Sub Button8_Click()
Dim strBaseCmd As String
Dim r1 As Range
Dim r2 As Range
Dim r3 As Range
Dim r4 As Range
Dim r5 As Range
Dim strParameter As String
Dim strCmd As String
' base command
strBaseCmd = "CMD /K"
' get a cell from a sheet in this workbook
Set r1 = ThisWorkbook.Worksheets("Create Users Home Drive").Range("d6")
Set r2 = ThisWorkbook.Worksheets("Create Users Home Drive").Range("d7")
Set r3 = ThisWorkbook.Worksheets("Create Users Home Drive").Range("d8")
Set r4 = ThisWorkbook.Worksheets("Create Users Home Drive").Range("d9")
Set r5 = ThisWorkbook.Worksheets("Create Users Home Drive").Range("d10")
strParameter = r1.Value & " " & r2.Value & " " & r3.Value & " " & r4.Value & " " & r5.Value
' add parameter to command
strCmd = strBaseCmd & strParameter
' run command
Shell strCmd, vbNormalFocus
End Sub
Do you have any idea on it, thank you very much for helping. 您有任何想法吗,非常感谢您的帮助。
You encounter the issues because of the way you pass the parameters. 由于传递参数的方式,您会遇到问题。 I don't see a space between /K
and first parameter you send to cmd. 我看不到/K
和发送给cmd的第一个参数之间的空格。 I believe that your first command looks like: 我相信您的第一个命令如下所示:
CMD /Kmd \\myPath
I would suggest you the following: 我建议您以下:
Either loop through your cells with while
or do while
and run the CMD command for each cell that has a value. 使用while循环遍历您的单元格,或者使用while
循环do while
并对每个具有值的单元格运行CMD命令。 If you don't like the multiple CMD windows, you can create a bat file and execute that one. 如果您不喜欢多个CMD窗口,则可以创建一个bat文件并执行该文件。
Or use mkdir function which is part of the VBA instead of CMD command. 或使用VBA中的mkdir函数代替CMD命令。 Mkdir documentation . Mkdir文档 。
Dim I as integer
I = 6 'Start with D6
While ThisWorkbook.Worksheets("Create Users Home Drive").Cells(I,4).Value <> ""
MkDir ThisWorkbook.Worksheets("Create Users Home Drive").Cells(I,4).Value
I = I + 1
Wend
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.