0

The python script that I have written works in VS Code. it both writes to the terminal when prompted as well as writes a new excel file (which is the goal). However, when run in excel, the script runs and then does not do anything. The terminal flashes but the code does not execute

When attempting this from excel, I have ensured that all excel files (and even vs code) were closed. The macro to run the code is in a separate file. What am I missing? Thanks!

Python

import pandas as pd 
import nameSplitter

Names = nameSplitter
to_drop = ['Question 2']

Names.df.drop(columns=to_drop, inplace=True)

sortedValues = Names.df.loc[Names.df['School Site'] == 3].sort_values('Question 1')

sortedValues.to_excel('sortedValues.xlsx', index=None)

VBA

Sub RunPythonScript()
'Procedure to run a Python Script in a VBA Procedure using the shell
 
    'Declaration
    Dim objShell As Object 'For the Shell
    Dim Pythonexe, PythonScript As String
     
    'Create the Shell Object
    Set objShell = VBA.CreateObject("Wscript.Shell")
     
    Pythonexe = """C:\Users\natha\AppData\Local\Programs\Python\Python310\python.exe""" 'path of the python.exe
    PythonScript = "C:\Users\natha\OneDrive\Desktop\form-filters-master\form-filters-master\project\QuantitativeData.py" 'path of your Python script
     
    'Run your Python script
    objShell.Run Pythonexe & " " & PythonScript 'run takes two arguments
 

     
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
nathan d
  • 1
  • 1
  • 1
    Define "Does not work". It's not possible for us to know what that means. – ApplePie Aug 31 '22 at 20:53
  • Same question/problem as https://stackoverflow.com/q/73561233/17172829 ? – Shrotter Aug 31 '22 at 20:56
  • When I run the excel module, a terminal opens for a split second and then closes. – nathan d Aug 31 '22 at 20:58
  • Shrotter, that looks like the same general question, however the suggested result did not fix this issue. – nathan d Aug 31 '22 at 21:09
  • 2
    Solved: For whatever reason ( I will research later) the code that I have would only work reading from and writing to an xlsx file. I was attempting to read from an xlsm file. – nathan d Aug 31 '22 at 21:30
  • 1
    If you want so see output prepend `cmd /k ` to `pythonpath`. Consoles close when the last program using it closes. – Lundt Aug 31 '22 at 21:59

0 Answers0