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
 

Excel Facts

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

Comment out the line:

Code:
On Error Resume Next

run the code, and see what happens.

Cheers

pvr928
 
Upvote 0
Type

Code:
Option Explicit

at the top of the code module. This forces you to declare all of your variables.

Run the code after having done this and let me know what happens.

PS you don't have to include a quote each time you reply.
 
Upvote 0
Ok

Uncomment the

Code:
Debug.Print

statements, show the 'Immediate Window' [VBE - View - Immediate Window], then step through the code using 'F8'.

See whether the output from the Debug.Print statements match exactly the folder structure and file names you are wanting to manipulate.

Also, change:

Code:
objFSO.movefile sourcelocation, Destination

to

Code:
objFSO.MoveFile (sourcelocation, Destination)
 
Upvote 0
Have you included a scripting reference to the file object?
 
Last edited:
Upvote 0
Below part is not working, im encountering compile error
Code:
objFSO.MoveFile (sourcelocation, Destination)

How do I do this? Sorry I'm quite new to vba
Have you included a reference to the file object?

I tried everything you said, but still, nothing is happening.. there's no debug error but nothing is happening. File is still outside the folder
 
Upvote 0
Are you sure you have included the statement

Code:
Option Explicit

at the top of your code?
 
Upvote 0
Tried it again,

I encountered error on line below, specific for objFSO. It said variable not defined
Code:
Set objFSO = CreateObject("scripting.filesystemobject")
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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