Copy Folder from C:\ and rename using multiple cell values

donnieFranklin

New Member
Joined
Feb 13, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I'm trying to setup macro on spreadsheet to copy a folder from my C:\Users\dfranklin\desktop\New folder and rename the folder based on the value from Multiple cells in the same Row., Would like to have a simple way to run the command on selected row.

Example
C:Users\dfranklin\desktop\10256_My Example_Ny City_Usa
10256​
My ExampleNy CityUsa
 
Now the code runs but does not create a folder. Message box does show and i select a cell. THen nothing happens and no new folder in the rootpath folder.
You must be looking at the wrong folder because your seeing the msgbox show up means there IS a folder created.
The path of the created folder is shown in the immediate window so check it out.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
The message box i'm seeing it the select a cell.
I did notice see in the code you wrote a print file path. I do not get a file path.
1613425915511.png


1613426059921.png

1613426103264.png

1613426139151.png

After this i get nothing.
 
Upvote 0
The path is shown in the IMMEDIATE WINDOW. Press Ctrl + G on the code screen.
Also, the folder you're trying to copy is named "New Folder", not "New folder" with the "f" in lower case.
 
Upvote 0
The path is shown in the IMMEDIATE WINDOW. Press Ctrl + G on the code screen.
Also, the folder you're trying to copy is named "New Folder", not "New folder" with the "f" in lower case.
Changed the folder name.
Here's what the immediate window screen shot. Same result after i changed the folder name
1613426961513.png
 
Upvote 0
"Path not found" or "Folder has been created", which message do you get after running the macro?
Try running the macro again and see which one you get.
 
Upvote 0
"Path not found" or "Folder has been created", which message do you get after running the macro?
Try running the macro again and see which one you get.
Here are the Steps in screenshots. I'm not getting any message after running the macro



1613427432849.png
1613427466569.png


After i select a cell and click ok
1613427499919.png
 
Upvote 0
This may sound like a stupid question but are you sure you hit "OK" (or press Enter), not "Cancel" after selecting a cell?
If you cancel your selection in the input box you're directed to errhandler, which triggers no event.
 
Upvote 0
No stupid questions here. But Yes just to make sure i tried again and made sure i hit okay. But this will seem even more interesting. I did get it to work but only on a random couple rows. Ive selected different rows and cells and it worked only a couple random times. But when it worked it was perfect.
1613428759751.png
 
Upvote 0
I think i figured it out. Its the / in some of the names. It don't like that.
That's it. Here's a workaround:
VBA Code:
Sub CopyFolderAndRename()
    Dim fPath As String, rootPath As String, folderName As String, r As Range
  
    rootPath = "C:\Users\dfranklin\desktop\" 'File address without file name
    folderName = "New folder" 'The name of folder to copy
  
    Set oFSO = CreateObject("Scripting.FileSystemObject")
  
    If oFSO.FolderExists(rootPath & folderName) = False Then
        MsgBox "Path not found.", vbExclamation, "Error"
    Else
        On Error GoTo errhandler
        Set r = Application.InputBox("Select a cell in a row after which you'd like to name the folder you're creating.", "Folder Name Choice", Type:=8)
  
        If r.Rows.Count = 1 Then
            fPath = rootPath & Range("A" & r.Row) & "_" & Range("B" & r.Row) & "_" & Range("C" & r.Row) & "_" & Range("D" & r.Row)
            If InStr(fPath, "/") > 0 Then
                fPath = Replace(fPath, "/", " ")
            End If
            oFSO.CopyFolder rootPath & folderName, fPath
            MsgBox "Folder has been created : " & vbCrLf & vbCrLf & fPath, vbInformation, "Notification"
            Debug.Print fPath 'In case you lose sight of the created folder
        Else
            MsgBox "Please select a cell in one row.", vbExclamation, "Error"
        End If
    End If
  
Exit Sub
errhandler: 'MsgBox "Error has occured.", vbExclamation, "Error"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,867
Members
449,053
Latest member
Mesh

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