Write Folder Name to cell (Need Code Correction)

Sleeplol

Board Regular
Joined
Apr 10, 2019
Messages
194
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,

I'm attempting to select a folder and have that folder file path write to the left of the active cell and for the folder name to write to the active cell.
However, it is not writing the Folder Name.
Please help me get this fixed.
Thanks

VBA Code:
Sub WritePathAndName()
Dim FldrPicker As FileDialog
    Dim myFolder As String
    Dim myFile As String
    

    'Have User Select Folder to Save to with Dialog Box
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
        myFolder = .SelectedItems(1) & "\"
    End With
    
    'Get the selected file name
    myFile = Dir(myFolder)
    
    'Write the folder path in the cell to the left of the active cell
    ActiveCell.Offset(0, -1) = myFolder
    
    'Write the selected file name in the cell to the right of the active cell
    ActiveCell.Offset(0, 0) = myFile
   
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi @Sleeplol .

I don't understand what you want to do with the macro.

Do you want to select a file and in one cell put the name of the file and in another cell the path of that file?
Because this dialog is for selecting folders only, it does not allow you to select files.

Could you explain with a very basic example what you need.
 
Upvote 0
Hi Dante,
Thanks for getting back. I must have to update my email or something, I wasn't alerted of a response.

Ok, I realized that is one of my older renditions of the code and I misspoke what I'm looking for.

What I need is to be able to click on a cell
1) Drive Explorer window opens
2) When Folder is selected the folder path is written to the left of the active cell
3) the Folder name is written in the active cell
 
Upvote 0
Could you explain with a very basic example what you need.
You forgot to put a very basic example, without the example I can only assume what you want in each cell.
Then try this:

VBA Code:
Sub WritePathAndName()
  Dim myFolder As String, sNames As Variant
  
  With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then Exit Sub
    myFolder = .SelectedItems(1)
  End With
  ActiveCell.Offset(0, -1) = myFolder
  sNames = Split(myFolder, Application.PathSeparator)
  ActiveCell = sNames(UBound(sNames))
End Sub
 
Upvote 0
Solution
Oh, my mistake on the example.

Your script does EXACTLY what I need!!!!

Wow yes. I've tried so many things and lame workarounds with formulas before finally coming here.

Many Thanks Dante
 
Upvote 1
Genius using the Split function to return the Name. That is what I was missing smh
 
Upvote 1

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top