Link to home
Start Free TrialLog in
Avatar of Dale Logan
Dale LoganFlag for United States of America

asked on

Run script from within Excel

Experts,

I received a solution to the following question that works perfectly. However, to get the results, a script must be run from a command prompt. Is there anyway to have this done from within Excel?

Previously asked question: Solution

Command lines to run script:
cd /d C:\Scripts\GetHandicap
powershell.exe -ExecutionPolicy Unrestricted -File GetHandicap.ps1

Thanks, Dale
Avatar of Kanti Prasad
Kanti Prasad

Hi

Try this

Private Sub CommandButton1_Click()

    Dim strProgramName As String
    Dim strArgument As String

    strProgramName = "C:\Scripts\GetHandicap\powershell.exe"
   
    strArgument = " -ExecutionPolicy Unrestricted -File GetHandicap.ps1"

    Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)

End Sub
To-run-from-excel.xlsm
Avatar of Dale Logan

ASKER

I get an error message "File not found." on the "Call Shell" line. I've checked for typos and don't find any. I have attached a screen shot of the file location. Again, I really don't know what's going on here since someone else created this for me.

User generated image
User generated image
ASKER CERTIFIED SOLUTION
Avatar of zalazar
zalazar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
zalazar,

Just so you know, this runs correctly. However, there is no way to know when the process has finished. The Command window stays open. Only when I close it, does the data populate in Excel. So, I just have to wait long enough before closing the window. Is there anyway for the process to know it has finished and then have it close the Command window?
That's indeed not so easy then. I actually do not have the problem myself.
While looking at it, things could probably be done better and therefore I have written some Excel VBA code that does the same as the PowerShell script.
You can also specify the users in the Excel sheet now instead of a txt file.
I have attached an Excel workbook that you can use.

Within the workbook you have to fill in the correct parameters in column B.
From the script directory curl.exe is used.
The users column, start and end row have to be defined.
It's not a problem if you put in a higher number for "Users Row End" as empty cells are automatically skipped.
Hopefully this works without any problem and hopefully it also makes the process easier.
GetHandicap-v10.xlsb
zalazar,

This is awesome. Thank you so much.

Dale
You're most welcome.