Code is not working

VeeBa

Board Regular
Joined
Apr 22, 2017
Messages
82
Hello Experts - I have below code which transfers the file (Gross Contribution Report) inside the subfolder (and override the same file inside this subfolder). However, nothing is happening whenever i'm running below code. Can someone help me understand what's wrong. For visualization purposes. I have a main folder named "Reports" inside this folders are subfolders. And inside these subfolders, there's a file (Gross Contribution Report) and another subfolder named "Previous FF". What I wanted to do is to transfer the file inside the Previous FF folder and override the file inside it.


Code:
Sub Test()
Dim F As String
Dim Folder As String
Dim path As String
Set objFSO = CreateObject("scripting.filesystemobject")
mFolder = Environ("USERPROFILE") & "\Desktop\Reports\" '-----------------do not need to change this path----
Set mainfolder = objFSO.GetFolder(mFolder)
On Error Resume Next
For Each mySubfolder In mainfolder.SubFolders '--this will give you the path of subfolders-----
'Debug.Print mysubfolder
cname = Right(mySubfolder, Len(mySubfolder) - Len(mFolder)) '----to find out the country name----
'Debug.Print cname
F = "Gross Contribution Report" '----mention the name of your file---
sourcelocation = mySubfolder & cname & F & ".xlsx"
Destination = mFolder & cname & "\Previous FF\" '----This is the destination for archive file which I have taken inside country folder------
'Debug.Print sourcelocation
'Debug.Print Destination
objFSO.movefile sourcelocation, Destination
Next
End Sub
 
That is exactly what I expected, and I'm pretty sure you didn't include that despite telling me you had.

To ensure good coding practice and to greatly assist with debugging, it is essential to have the statement:

Code:
Option Explicit

at the top of your code module.

Now that it is clear that many variables were not declared in the original code, try this:

Code:
Option Explicit

Sub Test()

Dim objFSO As Object
Dim F As String
Dim Folder As String
Dim path As String
Dim mainFolder As Object
Dim mFolder As String
Dim mySubfolder As Variant
Dim cname As String
Dim sourcelocation As String
Dim destination As String

Set objFSO = CreateObject("scripting.filesystemobject")
mFolder = Environ("USERPROFILE") & "\Desktop\Reports\" '-----------------do not need to change this path----
Set mainFolder = objFSO.GetFolder(mFolder)

'On Error Resume Next

For Each mySubfolder In mainFolder.SubFolders '--this will give you the path of subfolders-----
Debug.Print mySubfolder

cname = Right(mySubfolder, Len(mySubfolder) - Len(mFolder)) '----to find out the country name----
Debug.Print cname

F = "Gross Contribution Report" '----mention the name of your file---

sourcelocation = mySubfolder & "\" & F & ".xlsx"
Debug.Print sourcelocation

destination = mFolder & cname & "\Previous FF\" '----This is the destination for archive file which I have taken inside country folder------
Debug.Print destination

objFSO.MoveFile Source:=sourcelocation, destination:=destination

Next

End Sub
 
Last edited:
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
sorry about that. It worked. However, I encountered another error on below line, saying the file already exists. How can I make it override the file inside the Previous FF folder? Because I need the macro to override it monthly..

Code:
objFSO.MoveFile Source:=sourcelocation, destination:=destination
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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