+ Reply to Thread
Results 1 to 6 of 6

Copy File To Same Parent Folder

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Copy File To Same Parent Folder

    Hi,

    I have a script that has the following line which is not working.

    Please Login or Register  to view this content.
    What I basically want is, copy a file and paste it there (In the folder where the original file is) itself.

    The motive behind this is to create a copy of the file.

    However, when I change the path to some place else, it works, but it does not create a copy in the same folder itself. Can anyone guide.

    To understand what I mean, put a dummy file is a folder. Copy the file, and paste it there itself. This is the action I am trying to automate.
    Cheers!
    Deep Dave

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Copy File To Same Parent Folder

    I'm assuming this is using File System Object. If yes, give the file a new unique file name because it's in the same folder as the original. The code below inserts a (2) to the file name.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Copy File To Same Parent Folder

    Hi AlphaFrog, thank you for the reply. I had actually figured that out.

    I just want to know if there is a way as simple as pasting the file, and it will automatically get the next number.

    In your solution, (2) is hard coded, and if a file already exists with that name, it will error out.

    However, when you copy a file in a folder and keep hitting Ctrl+V it automatically gets the next number.

    I want to replicate this behaviour using VBA.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Copy File To Same Parent Folder

    I don't know of a built-in "automatic" method to sequentially number the files. You could have the code loop through all the files in the folder to find the max sequential number and then increment it by 1 for the next save. Alternatively, a common practice is to concatenate the Date-Time to the file name to make it unique.

  5. #5
    Forum Contributor IonutC's Avatar
    Join Date
    01-28-2016
    Location
    Bucharest, Romania
    MS-Off Ver
    Office 2019
    Posts
    486

    Re: Copy File To Same Parent Folder

    Hi Need,

    It is simple to use a code like the one below:

    Please Login or Register  to view this content.
    the code from above is an example. you can customize yours in order to have a specified path to save these files. In my example I have a folder on my desktop named "MacroFiles".
    Please consider:
    Be polite. Thank those who have helped you.
    Click the star icon in the lower left part of the contributor's post and add Reputation. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .
    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Posting code between tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

  6. #6
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Copy File To Same Parent Folder

    @ALphaFrog - Thank you for the help..

    @IonutC - Thank You For The Help! However, I know that bit you told me, just wanted a know of a way to replicate the behaviour mentioned in Post #3 Thank you anyways.

    Reps added to both.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Get Values of a cell from a file in a parent folder
    By plans in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-04-2015, 12:23 PM
  2. Copy data fom parent file to child files with different layouts
    By Marloes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-14-2014, 05:12 AM
  3. [SOLVED] VBA Code to find a Folder name within a Parent folder that contains defined text
    By knowga in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-24-2014, 05:17 AM
  4. Open a file from parent folder of the activeworkbook
    By alazari in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-26-2013, 06:38 AM
  5. search for a file in a folder based on data, copy and paste it in another folder
    By kboy1289 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 11:47 AM
  6. Move Excel file to parent folder and add suffix with VBA Macro
    By clogwyn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-21-2013, 05:15 PM
  7. [SOLVED] get the parent folder of the working folder
    By tom taol in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-15-2005, 12:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1