No error but result missing

N_Reef

New Member
Joined
Mar 16, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello, may someone can help me ?...
The following code is somehow running threw without any error message and I get as well the MsgBox for the right year as the MasgBox "Done". But in the end no folder was copied to the destination.

I have the feeling it is something wrong with the part of mydate/ MeinDatum. Somehow it can not find it ?
Could it be that it can not find the list including the country file names anymore as I added more lines and therewith the list moved? If yes how can I link it correctly again?

Thank you a lot in advance. This topic is hanging here since weeks.
BR, Nadine

VBA Code:
Sub Copy_and_Rename_Files()

    Dim sourceFolder As String, destinationYearFolder As String
    Dim copyFileNames As Variant, copyFileName As Variant
    Dim subfolderName As String
    
    Dim MeinDatum As String
    MeinDatum = Range("G4").Value
    MsgBox MeinDatum
       
    sourceFolder = "C:\Users\N\OneDrive \umbenennen der Master files IMPROVEMENT Project\Master Files\"      'CHANGE THIS
    destinationYearFolder = "C:\Users\N\Desktop\umbenennen der Master files IMPROVEMENT Project\02_Sent out Files " & MeinDatum  'CHANGE THIS"

    copyFileNames = Split("MASTER_O_Bs" & MeinDatum & ".xlsm, MASTER_O_Ds" & MeinDatum & ".xlsm, MASTER_O_H" & MeinDatum & ".xlsm, MASTER_O_P" & MeinDatum & ".xlsm, MASTER_O_T" & MeinDatum & ".xlsm", ",")
       
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destinationYearFolder, 1) <> "\" Then destinationYearFolder = destinationYearFolder & "\"
   
    subfolderName = Dir(destinationYearFolder, vbDirectory)
    While subfolderName <> vbNullString
        If (GetAttr(destinationYearFolder & subfolderName) And vbDirectory) = vbDirectory Then
            If subfolderName <> "." And subfolderName <> ".." Then
                For Each copyFileName In copyFileNames
                    Filecopy sourceFolder & copyFileName, destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_"))
                Next
            End If
        End If
        subfolderName = Dir()
    Wend

    MsgBox "Done"
   
End Sub
 
Last edited by a moderator:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Just a quick look at the macro since there is no other information on folder paths and names:
"C:\Users\N\OneDrive \umbenennen Is the space after OneDrive correct ?
Project\02_Sent out Files " & Is the space after Files correct ?
 
Upvote 0
t the macro since there is no other information on folder paths and na
Ah sorry, the path I copied with path copy so it should be absolutely fine. For here I just removed manually some information out of the path name as these were not meant for public. *
 
Upvote 0
Try adding some Message Boxes into the code before the FileCopy part to verify you are creating valid file path/names, i.e.
amend this:
VBA Code:
For Each copyFileName In copyFileNames
                    Filecopy sourceFolder & copyFileName, destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_"))
                Next
to this:
VBA Code:
For Each copyFileName In copyFileNames
    MsgBox sourceFolder & copyFileName, vbOkOnly, "Source File"
    MsgBox destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_")), vbOkOnly, "Destination File"
    Filecopy sourceFolder & copyFileName, destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_"))
Next
If that does not reveal the problem to you, please post back here with what each of those Message Boxes returns.
 
Upvote 0
MsgBox sourceFolder & copyFileName, vbOkOnly, "Source File" MsgBox destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_")), vbOkOnly, "Destination File"
I entered the two lines like you wrote. There is nothing new happening. I still get the MsgBox with the year 2023 and then the "Done" MsgBox.
 
Upvote 0
I entered the two lines like you wrote. There is nothing new happening. I still get the MsgBox with the year 2023 and then the "Done" MsgBox.
I tried it with entering the source & Final destination of the master files and with leaving the text 100% how you sent it
 
Last edited:
Upvote 0
If you entered the code like I showed you, you should get two message boxes, one for the Source Path and File Name you are building, and one for the Destination Source Path and File Name you are building.
What do those two Message Boxes return?
Please post the entire result they return.

If you are not getting any, then either your code is erroring out before you get there, or there are no values in "copyFileNames" to loop through.
 
Upvote 0
Try adding some Message Boxes into the code before the FileCopy part to verify you are creating valid file path/names, i.e.
amend this:
VBA Code:
For Each copyFileName In copyFileNames
                    Filecopy sourceFolder & copyFileName, destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_"))
                Next
to this:
VBA Code:
For Each copyFileName In copyFileNames
    MsgBox sourceFolder & copyFileName, vbOkOnly, "Source File"
    MsgBox destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_")), vbOkOnly, "Destination File"
    Filecopy sourceFolder & copyFileName, destinationYearFolder & subfolderName & "\" & Left(subfolderName, InStr(subfolderName, "_") - 1) & Mid(copyFileName, InStr(copyFileName, "_"))
Next
If that does not reveal the problem to you, please post back here with what each of those Message Boxes returns.
OKay I changed a little thing in the path and now I get a MsgBox showing my path twice and then the next box includes following (ScreenShot attached)
 

Attachments

  • Fehler 5.jpg
    Fehler 5.jpg
    21.5 KB · Views: 8
  • path.jpg
    path.jpg
    27.5 KB · Views: 7
Upvote 0
On the error in the first one, if you click the "Debug" button, which line of code does it show?

If your second message box shows two file paths, that is an issue, as it should only be showing one.
So you have some editing to do to fix it. You can use the MsgBox to make sure it is showing what you need.
 
Upvote 0
On the error in the first one, if you click the "Debug" button, which line of code does it show?

If your second message box shows two file paths, that is an issue, as it should only be showing one.
So you have some editing to do to fix it. You can use the MsgBox to make sure it is showing what you need.
First of all, thank you for your help so far!! We got already one step further.

To your question: The second one of the two you gave me is lightened.
Oh okay,.. so I have to fix things here. But I don't understand how or what. ?
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,809
Members
449,048
Latest member
greyangel23

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