Home » excel » excel – How do i get my "terminal" commands to run in the right folder from vba (on OSX)

excel – How do i get my "terminal" commands to run in the right folder from vba (on OSX)

Posted by: admin April 23, 2020 Leave a comment


I have an excelfile (Excel 2011, running on OSX 10.9.4) which exports its data into a textfile. I want to automatically upload this textfile to Git once it is exported.

I use the execShell function described in this post:
VBA Shell function in Office 2011 for Mac
Which gives me nice output for my executed commands.

I want to go to my folder (where my new file was put) and from there call my git commit commands (“git add *”, “git commit -m commitmessage” and “git push”). But the problem is i cannot get to my folder. If i try cd /Users/username/folder and then pwd i keep getting back “/ ” as location (from where i cannot call my git commands)

the complete code for this looks like this:

commitMessage = """New feature file: " & ActiveSheet.Name & """"
gotoPath = Replace(Replace(CurDir(), ":", "/"), "Macintosh HD", "") 'make a "normal" path from CurDir()
result = execShell("cd " & gotoPath, exitCode)
result = execShell("pwd", exitCode)
result = execShell("git add *", exitCode)
result = execShell("git commit -m """ & commitMessage & """", exitCode)
result = execShell("git push", exitCode)

If i execute my git commands from the terminal myself (while in the right folder), the commit works fine.
If i execute a “say” command with the execShell function, it works fine as well

Anybody have any suggestions about how to get to the right location? or alternatives to use for this purpose?

How to&Answers:

Each time you call execShell it starts a new shell. That shell executes the command and then exits. So your new shell does change directory for that shell then exits. The next new shell you exec starts in exactly the same place your last one started – not where it exited because it is a new, different process.

You need to do all things in one shell:

execShell("cd somewhere && do something && do something else")

The above will only do each subsequent command if the previous was successful, If you want to do additional commands unconditionally, regardless of whether the previous one failed, separate the commands by a semi-colon:

execShell("cd somewhere; do something; do something else")