Thursday, June 30, 2016

How select folder using excel VBA code?

Many a times we need to select folder to get input data path during project automation. Which we will be the first activity before macro execution. Selecting folder using dialogue box is very vital task any any automation activity. it helps as a input data validation to use correct input data in further execution. Also, it help to achieve data quality in report automation task.




Folder selection can be implemented by writing a short VBA code,


Here is the code which show folder selection dialogue box:

Sub Select_Folder()
' Author: Dreams24
' Written for VBA Tricks and tips blog
http://vbatricksntips.blogspot.com
  
   'Declare Variables
    
    Dim diaFolder As FileDialog
        
    ' Open the file dialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show

    ' Message box to show selected folder path
    MsgBox "Folder Path is: " & diaFolder.SelectedItems(1)
    
    Set diaFolder = Nothing

End Sub

No comments: