I have read several threads and found that I can call Python from VBA using Shell.
This is what I have:
Private Sub CommandButton1_Click()
Ret_Val = Shell("C:\Users\....\Anaconda3\python.exe C:\Users\...\REL.py", vbNormalFocus)
If Ret_Val <> 0 Then
MsgBox "ok!", vbOKOnly
End If
End Sub
I do get the OK message, and a window open and close within a second. So my guess it is launching Python but the script does not work.
So I thought that maybe does not work because I wrote the code in spyder and I might need to launch spyder:
Ret_Val = Shell("C:\Users\...\Anaconda3\Scripts\spyder.exe C:\Users\...\REL.py", vbNormalFocus)
This will open Spyder, which takes too much time and it is not what I want. Also once it is opened, it does not launch the script.
Something is wrong, but I am not sure if it is due to the VBA code or something else related to the script. The script works when I run it from Spyder without VBA.
This is a simple example of the Python code:
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 21 08:24:05 2017
@author: xxxxxx
"""
import pandas as pd
import numpy as np
import datetime
RW = pd.read_excel('Raw_data.xlsx', skipinitialspace=True)
RW=pd.DataFrame(RW, columns = ['Task Id', 'TIDmodRW', 'Start Date', 'End Date', 'Machine', 'Boards'])
writerRW = pd.ExcelWriter('RW_test2.xlsx', engine='xlsxwriter')
RW.to_excel(writerRW, sheet_name='sheet1')
writerRW.save()
input("Press any key to close")so that you can view the result before the window closes. By judicious use ofprint(), can you identify the line in the Python code which is throwing the error?Shell("C:\Users\....\Anaconda3\python.exe ""C:\Users\...\REL.py"" ", vbNormalFocus)Double-up the quotes to escape them as shown.