Home>Blogs>VBA>Shell Function in VBA with Example
Shell Function in VBA
VBA

Shell Function in VBA with Example

Shell function in VBA is very useful. If you want to start a program or open a executable file then you can use shell function in VBA . In this article we will explain shell command for below 3 examples-

  • Start a Program
  • Open a Folder
  • Open a Non-Excel file
  • Below is the syntax of Shell function-

Shell Function in VBA

Shell (pathname, windowstyle)

Pathname: The full path of the program to execute (String).

WindowStyle: Windowstyle is an optional argument. If it is omitted, the program starts minimized with focus.

Below is the list of windowstyle Constant and numeric values –

ConstantValueDescription
vbHide0The window is hidden, and focus is passed to the hidden window.
vbNormalFocus1The window has focus and appears in its most recent size and position.
vbMinimizedFocus2The window is minimized but has focus. This is default window style.
vbMaximizedFocus3The window is maximized and focused.
vbNormalNoFocus4The window appears in its most recent size and position, and the currently active program will be focused
vbMinimizedNoFocus6The window is minimized, the currently active program will be focused.

 

Start a Program:

In the path name you type the program name like, Notepad, Calc, WinWord etc.

VBA.Shell "Notepad", vbNormalFocus

 

Open a Folder:

To open a folder, we need to use “Explorer.Exe FolderPath” as given in below example-

VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files", vbNormalFocus

 

Open a Non-Excel File:

To open a Non-Excel File, we need to use “Explorer.Exe FolderPath/FilenamewithExtension” as given in below example-

VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files\Test-1.pdf", vbNormalFocus

 

 Click here to download the Practice file

Watch the step by step video tutorial:

 

Visit our YouTube channel to learn step-by-step video tutorials

Youtube.com/@PKAnExcelExpert

 

PK
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!
https://www.pk-anexcelexpert.com