• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Prompt user to open file with a dialogue box

ThrottleWorks

Excel Ninja
Hi,

I am using below mentioned set codes to prompt user to select the file, and open the file user has selected.

I am not facing any issue with this code, however just wanted to know is it a safe (proper) code. Can anyone please help me in this. Will I face any problem with code in future.

PS - This is not an issue, just wanted to check if the code is correct. No urgency.

Code:
Sub Button_1()
    Dim strPath As String
    strPath = SelectFile(ThisWorkbook.Worksheets("Macro").Range("B2").Value)
    ThisWorkbook.Worksheets("Macro").Range("B2").Value = strPath
End Sub

Workbooks.Open MacroSht.Range("B2").Value
Set MyBook = ActiveWorkbook
 
Am really sorry. Will upload shortly. Missed it by mistake.

Code:
Sub Button_1()
    Dim strPath As String
    strPath = SelectFile(ThisWorkbook.Worksheets("Macro").Range("B2").Value)
    ThisWorkbook.Worksheets("Macro").Range("B2").Value = strPath
End Sub


Function SelectFile(strTitle As String, Optional isFolder As Boolean) As String
    SelectFile = ""
 
    If isFolder = True Then
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = strTitle
            .Show
         
            If .SelectedItems.Count = 1 Then
                SelectFile = .SelectedItems(1)
            End If
        End With
        Exit Function
    End If
 
    With Application.FileDialog(msoFileDialogOpen)
        .Title = strTitle
        .Show
     
        If .SelectedItems.Count = 1 Then
            SelectFile = .SelectedItems(1)
        End If
    End With
End Function


Code:
Sub File_Open()
'Open Excel files based on the cell value
Application.EnableEvents = False
Application.DisplayAlerts = False
Workbooks.Open MacroSht.Range("B2").Value
Set MyBook = ActiveWorkbook


Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
 
Last edited:
@ThrottleWorks

Sorry, had missed the edit on your post.

Only thing I'd suggest is to disallow multiselect like below (i.e. only allow one file to be selected by the user).

Code:
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
 
Back
Top