3

I am trying to run VBA script that will execute particular shell program with arguments retrieved from Excel Spreadsheet.

I'd like to exeute the command n times for n rows in spredsheet, yet have it all in one cmd.exe window. Is that achievable?

I am doing such steps:

1.Creating shell object:

Set wsh = VBA.CreateObject("WScript.Shell")

2.Iterating trough all rows in Spreadsheet and calling for each:

wsh.Run("Command with row specific args", 1, true)

As the result I have n console windows opened for n processed rows. Is there any way to avoid it?

Help appreciated.

0

1 Answer 1

3

You don't give much details, but a solution could be to build a batch (.bat) file (no different from writing to any other text file) with all the parameters you need, save it somewhere and then run it in a single call to cmd.exe. And then you can just delete that file.


Start with opening the file for output:

Dim fn As Long
fn = FreeFile
Open "filename.bat" For Output As #fn

Then iterate your range, and instead of launching a cmd.exe for each, write to your open file:

Print #fn "command with row specific args"

Then close your file and run it:

Close #fn
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run("filename.bat", 1, true)

Then you can discard the file:

Kill "filename.bat"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.