macro to move files in the list

luvbite38

Active Member
Joined
Jun 25, 2008
Messages
368
Hi

I am using the following code to copy and move the files which are listed in the column B of worksheet called sheet2.

It is taking considerably long time to do the operation.

Is there another quickest way of moving the files or someone can kindly optimise the following code to make it work faster.

PHP:
Sub Setup4()
Application.ScreenUpdating = False
Dim myDir As String
Dim myDest As String
Dim myCell As String
Dim myCount As Single
'mycount is the starting row that the file names start at
myCount = 1
Do
'Change this to the Column you want to read
myCell = Range("B" & myCount)
'Change this to the location that the files are in
myDir = ThisWorkbook.Path
'Change this to the location the files are to be copied to
myDest = ThisWorkbook.Path & "\" & "Moment & Shear Outputs"
FileCopy myDir & "\" & myCell, myDest & "\" & myCell
myCount = myCount + 1
'Will loop until the first empty cell
Loop Until IsEmpty(Range("B" & myCount))
Application.ScreenUpdating = True
End Sub


Kind Regards,
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
For optimization:


If you use a range variable you can do away with recalculating myCount.
If you use a variable for the path you will only calculate ThisWorkbook.Path once.

For example
Code:
  [color=green]'start cell[/color]
  [color=darkblue]Set[/color] rng = [COLOR="Red"]Sheets("Sheet2").Range("B1")[/COLOR]
  myPath = ThisWorkbook.Path & "\"

We then loop through the column B until we find an empty cell.

Code:
  [color=green]'loop through column B until you find an empty cell[/color]
  [color=darkblue]Do[/color] [color=darkblue]Until[/color] rng = ""

Set up the source and destination file:
Code:
    myDir = myPath & rng.Value
    myDest = myPath & "Moment & Shear Outputs\" & rng.Value

If you want to move the file use the Name function:
Code:
    [color=green]'========================[/color]
    [color=green]'move and rename the file[/color]
    [color=green]'========================[/color]
    Name myDir [color=darkblue]As[/color] myDest

Copying the file leaving the original in the source folder will be the main time consuming part of the procedure:
Code:
    [color=green]'=======================[/color]
    [color=green]'copy and move the files[/color]
    [color=green]'=======================[/color]
    FileCopy myDir, myDest

Then we get the next file:
Code:
    [color=green]'next file[/color]
    [color=darkblue]Set[/color] rng = rng.Offset(1, 0)


The full code is:
NB the Name function is commented out
Code:
[color=darkblue]Sub[/color] SetUp4a()
  [color=darkblue]Dim[/color] rng [color=darkblue]As[/color] Range
  [color=darkblue]Dim[/color] myDir [color=darkblue]As[/color] [color=darkblue]String[/color]
  [color=darkblue]Dim[/color] myDest [color=darkblue]As[/color] [color=darkblue]String[/color]
  [color=darkblue]Dim[/color] myPath [color=darkblue]As[/color] [color=darkblue]String[/color]
  
  [color=green]'start cell[/color]
  [color=darkblue]Set[/color] rng = Sheets("Sheet2").Range("B1")
  myPath = ThisWorkbook.Path & "\"
  
  [color=green]'loop through column B until you find an empty cell[/color]
  [color=darkblue]Do[/color] [color=darkblue]Until[/color] rng = ""
    myDir = myPath & rng.Value
    myDest = myPath & "Moment & Shear Outputs\" & rng.Value

    [color=green]'========================[/color]
    [color=green]'move and rename the file[/color]
    [color=green]'========================[/color]
    [color=green]'Name myDir As myDest[/color]
    
    [color=green]'=======================[/color]
    [color=green]'copy and move the files[/color]
    [color=green]'=======================[/color]
    FileCopy myDir, myDest
    
    [color=green]'next file[/color]
    [color=darkblue]Set[/color] rng = rng.Offset(1, 0)
  [color=darkblue]Loop[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

Hope this helps,
Bertie
 
Last edited:
Upvote 0
PS

You can even do away with the myDir and myDest variables and put their contents directly into the FileCopy line.

I have used variables to make the code easier to read.
Bertie
 
Upvote 0
Hi, i know this post is really old, just hope someone reads this and can help me.
i am doing a very simular thing when i am wanting a macro to fime a list of pdf files listed in column b in a folder on my desktop to a new folder on my desktop. I cant seem to make the one above work for me.
 
Upvote 0
Hi Brian, welcome to the forum

It is an old post. Please note I haven't tested the advice I post below, but the logic is sound.
Here is what the code does.

Set up paths to the folders you are working with. For example, say you want to move files in the "Temp" folder to the "Temp" folder archive; you would use:
Rich (BB code):
  oldPath = "C:\Temp\"
  newPath = "C:\Temp\Archive\"

NB REMEMBER END BACKSLASH

Then you set up the range you are going to loop through. In this example Sheet2 starting at cell B1. EDIT IF NECESSARY
Rich (BB code):
  Set rng = Sheets("Sheet2").Range("B1")

Then you loop through the list until you reach an empty cell.
Rich (BB code):
 Do Until rng = ""

Build up the old and new FULL FILE PATHS
Rich (BB code):
      'build up the full path to the old file
      oldFile = oldPath & rng.Value & ".pdf"
      
      'build up the full path to the new file you want to create
      newFile = newPath & rng.Value & ".pdf"

NB If you don't need to add the ".pdf" file extension delete as highlighted (red)
rng.value is the value in the cell on the spreadsheet.


And copy the file to the new folder:
Rich (BB code):
      'copy the old file to the new new folder
      FileCopy oldFile, newFile


If we put all that together we get:

Rich (BB code):
Sub SetUp4a()
  Dim rng As Range
  Dim oldPath As String
  Dim newPath As String
  Dim oldFile As String
  Dim newFile As String
  
  '================================================
  'Set the paths to the folders you are processing
  'REMEMBER END BACKSLASH
  '================================================
  oldPath = "C:\Temp\"
  newPath = "C:\Temp\Archive\"
  
  '=======================================================
  'set up the start of the range you want to loop through
  'EDIT SHEET NAME AND FIRST CELL ADDRESS IF NECESSARY
  '======================================================
  Set rng = Sheets("Sheet2").Range("B1")
  
   '==================================================
  'loop through column B until you find an empty cell
  '==================================================
  Do Until rng = ""
      
      'build up the full path to the old file
      oldFile = oldPath & rng.Value & ".pdf"
      
      'build up the full path to the new file you want to create
      newFile = newPath & rng.Value & ".pdf"
  
      'copy the old file to the new new folder
      FileCopy oldFile, newFile
    
    'get the next file by moving down one row
    Set rng = rng.Offset(1, 0)
  Loop
End Sub

Hope this helps you understand the process,
Bertie
 
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,531
Members
449,515
Latest member
lukaderanged

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