Home » excel » vba – Execute command with Excel macro and close cmd window

vba – Execute command with Excel macro and close cmd window

Posted by: admin April 23, 2020 Leave a comment

Questions:

Here is what I’m trying right now:

Sub del_BJSFM_files()

Call Shell("cmd.exe /S /K" & "cd /d C:\UTAS-SA && del /f/s/q BJSFM > nul", vbNormalFocus)

End Sub

The problem is that the command window stays open.

I tried removing the “/K” but then the command doesn’t execute properly.

How to&Answers:

Here is code for it

Sub del_BJSFM_files()
  Call Shell("cmd.exe /S /c" & "cd /d C:\UTAS-SA && del /f/s/q BJSFM > nul", vbNormalFocus)
End Sub

I replaced /k with /c and now it closes the window.

Answer:

/C – Run Command and then terminate

/K – Run Command and then return to the CMD prompt. This is useful for testing, to examine variables

Source – https://ss64.com/nt/cmd.html