Home>Blogs>VBA>Shell Function in VBA
Shell in VBA

Shell Function in VBA

Shell Function in VBA can run executable program and returns a Variant (Double) representing the program’s task ID if successful otherwise, it returns zero.


Shell(pathname, [ windowstyle ])

pathname: Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.

windowstyle: Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run.

The windowstyle named argument has these values:

vbHide0Window is hidden and focus is passed to the hidden window. The vbHide constant is not applicable on Macintosh platforms.
vbNormalFocus1Window has focus and is restored to its original size and position.
vbMinimizedFocus2Window is displayed as an icon with focus.
vbMaximizedFocus3Window is maximized with focus.
vbNormalNoFocus4Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus6Window is displayed as an icon. The currently active window remains active


Below are the few examples of Shell function in VBA

Start a Program:

VBA.Shell "Notepad", vbNormalFocus

VBA.Shell "winword", vbNormalFocus

VBA.Shell "MSAccess", vbNormalFocus

Close a file

Dim my_notepad As Variant

my_notepad = VBA.Shell("Notepad", vbNormalFocus)

my_notepad = VBA.Shell("Taskkill /F /PID " & my_notepad, vbHide)

Open a Folder

VBA.Shell "Explorer.exe C:\Users\PK\Desktop\MyFolder\PDF Files", vbMaximizedFocus

Open a Non Excel File

VBA.Shell "Explorer.exe C:\Users\PK\Desktop\MyFolder\PDF Files\Test-2.pdf", vbNormalFocus


Click here to download the practice file.

Watch the step by step video tutorial:

Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!