Selecting File Path & inputting to cell
Results 1 to 4 of 4

Thread: Selecting File Path & inputting to cell

  1. #1
    New Member
    Join Date
    Aug 2016
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Selecting File Path & inputting to cell

    All -

    Beginning coder attempting to write macro to attach to a CommandButton to store a selected file path (folder or file from MS File Explorer) into a specific cell "B2".

    Currently having issues with the code below:

    Code:
    Sub GetFilePath2()
    Dim filename As String
    filename = Application.GetOpenFilename
    
    Dim cell As Range
    cell = Application.Range("B2")
    cell.Value = filename
    End Sub
    Issues:
    • The Open File Explorer pops up but does not allow you to select a folder, rather must "open" a file. Is there a different application than ".GetOpenFilename" I should be using?
    • Getting run-time error "'91': Object variable or With block variable not set"



    Thank you!
    Last edited by WingSystems; May 13th, 2019 at 03:07 PM.

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    26,841
    Post Thanks / Like
    Mentioned
    459 Post(s)
    Tagged
    45 Thread(s)

    Default Re: Selecting File Path & inputting to cell

    How about
    Code:
    Sub WingSystems()
       Dim Fldr As String
       
       With Application.fileDialog(4)
          .AllowMultiSelect = False
          If .Show = -1 Then Fldr = .SelectedItems(1)
       End With
       MsgBox Fldr
    End Sub
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    New Member
    Join Date
    Aug 2016
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Selecting File Path & inputting to cell

    Quote Originally Posted by Fluff View Post
    How about
    Code:
    Sub WingSystems()
       Dim Fldr As String
       
       With Application.fileDialog(4)
          .AllowMultiSelect = False
          If .Show = -1 Then Fldr = .SelectedItems(1)
       End With
       MsgBox Fldr
    End Sub
    Thank you Fluff, was looking for the .fileDialog application!

    How would you be able to then input the ".SelectedItems" into a Cell (B2)?

    Code below is giving me the variable not set error.

    Code:
    Sub GetFilePath2()
       Dim Fldr As String
       
       With Application.FileDialog(4)
          .AllowMultiSelect = False
          If .Show = -1 Then Fldr = .SelectedItems(1)
       End With
       MsgBox Fldr
       
       Dim cell As Range
       cell = Application.Range("B2")
       cell.Value = Fldr
       
    End Sub
    Last edited by WingSystems; May 13th, 2019 at 03:28 PM.

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    26,841
    Post Thanks / Like
    Mentioned
    459 Post(s)
    Tagged
    45 Thread(s)

    Default Re: Selecting File Path & inputting to cell

    Like
    Code:
    Sub WingSystems()
       Dim Fldr As String
       
       With Application.fileDialog(4)
          .AllowMultiSelect = False
          If .Show = -1 Then Fldr = .SelectedItems(1)
       End With
       Range("B2").Value = Fldr
    End Sub
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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