0

I have a file named control.xlsx and want to make macro in this file so control other files in same directory.

I tried like below,

Sub control()

  name = Dir(ActiveWorkbook.path & "\*.xlsx")
  Do While name <> ""
   FileCopy name, ActiveWorkbook.Path & "\new" & "\" & name
  Loop

End Sub

but it doesn't work.. anybody who can help me! I want to modify the copied excel files next. any tips!

tehoo
  • 81
  • 2
  • 9
  • 3
    Note that *"it doesn't work"* is a completely useless error description. Also make sure that `ActiveWorkbook` is exactly what you want and not `ThisWorkbook` they often get mixed up. • I also recommend to activate `Option Explicit`: In the VBA editor go to *Tools* › *Options* › *[Require Variable Declaration](https://www.excel-easy.com/vba/examples/option-explicit.html)*. – Pᴇʜ Jan 17 '19 at 08:07
  • 1
    Duplicate of https://stackoverflow.com/questions/16943003/vba-to-copy-a-file-from-one-directory-to-another – Sam Jan 17 '19 at 08:07
  • Are you sure the full destination path exists? – SPlatten Jan 17 '19 at 08:10
  • 1
    Possible duplicate of [VBA to copy a file from one directory to another](https://stackoverflow.com/questions/16943003/vba-to-copy-a-file-from-one-directory-to-another) – Michał Turczyn Jan 17 '19 at 08:11
  • Why not just duplicate the folder (with contents...) – Solar Mike Jan 17 '19 at 08:38
  • @SolarMike Good idea, but this would duplicate all files not only `xlsx`. – Pᴇʜ Jan 17 '19 at 09:03

1 Answers1

1

Your code is almost OK.

     Sub control()
        name = Dir(ActiveWorkbook.path & "\*.xlsx")
        Do While name <> ""
           FileCopy name, ActiveWorkbook.Path & "\new" & "\" & name
           name = Dir()
        Loop
     End Sub
Rafał B.
  • 487
  • 1
  • 3
  • 19