Results 1 to 3 of 3

Thread: Excel VBA Macro To Open A File Through Browse Dialog Box

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    39
    Rep Power
    0

    Excel VBA Macro To Open A File Through Browse Dialog Box

    I need a macro for the following task.
    1. ask for the file name through inputbox
    2. open the file if the name is correct otherwise give an error msgbox.
    3. the directory can be specified initially in the vba.

    Thanks.

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Rather asking for the filename, let the user select the file.

    Code:
    Sub kTest()
    
        Dim strFile     As String
        
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Title = "Select the file.."
            .InitialFileName = "D:\"        'adjust the default folder
            .Filters.Clear
            .Filters.Add "Excel Files", "*.xls*"
            If .Show = -1 Then
                strFile = .SelectedItems(1)
            Else
                MsgBox "No file selected", vbInformation, "ExcelFox.com"
                Exit Sub
            End If
            
            Workbooks.Open strFile, 0
            
        End With
                
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    39
    Rep Power
    0
    It worked perfectly. Thanks admin

Similar Threads

  1. Open PDF file with PowerPoint VBA
    By bg52ip in forum Powerpoint Help
    Replies: 4
    Last Post: 06-12-2013, 11:28 PM
  2. Replies: 3
    Last Post: 06-10-2013, 06:12 PM
  3. Open And Activate Workbook Before Runing Macro
    By Howardc in forum Excel Help
    Replies: 5
    Last Post: 06-04-2013, 07:23 PM
  4. Check if file is already open
    By Rasm in forum Excel Help
    Replies: 5
    Last Post: 11-24-2011, 04:55 PM
  5. Replies: 1
    Last Post: 06-02-2011, 10:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •