Consulting

Results 1 to 6 of 6

Thread: Open a File from a folder Excel Vba

  1. #1
    VBAX Regular
    Joined
    Jun 2012
    Posts
    11
    Location

    Open a File from a folder Excel Vba

    Hi all!!
    I need a code in Excel vba to open a folder and select a file and assign that file to a variable of object type.
    Please help!!
    Thanks in Advance

  2. #2
    Play with this one.

    [vba]

    Dim MyFolder As String
    Dim MyFile As String 'enter any variable name

    With Application.FileDialog(msoFileDialogFolderPicker)

    If .Show = -1 Then
    MyFile = Dir(.SelectedItems(1) & "\*.txt") ' edit file extension
    End If

    End With
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Jun 2012
    Posts
    11
    Location
    Hi Defcon...

    Thanx a lot for the quick reply!!

    But, Indeed i need a little more help.

    The code is giving an option to open a folder.
    I want to select a File from that folder & assign that file to an Object type variable. Can you help me modifying the code?


    Thnx a lot once again!!

  4. #4
    Assuming you were selecting a txtfile. This is how you assign the selected file to a variable: In this case the variable is MyFile. The selected file will be =/assigned to MyFile

    [vba]
    MyFile = Dir(.SelectedItems(1) & "\*.txt")
    [/vba]

  5. #5
    VBAX Regular
    Joined
    Jun 2012
    Posts
    11
    Location
    The variable should be of Object type.. not a string one!!!

  6. #6
    Can you elaborate the scenario? What exactly are you working on?

Posting Permissions

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