Selecting File Path & inputting to cell

WingSystems

New Member
Joined
Aug 24, 2016
Messages
13
Office Version
365
Platform
Windows
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:

Some videos you may like

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,943
Office Version
365
Platform
Windows
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
 

WingSystems

New Member
Joined
Aug 24, 2016
Messages
13
Office Version
365
Platform
Windows
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,943
Office Version
365
Platform
Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,843
Messages
5,489,216
Members
407,679
Latest member
Elaine Grass

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top