+ Reply to Thread
Results 1 to 5 of 5

Files to be moved to folder created by another macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-17-2012
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    122

    Question Files to be moved to folder created by another macro

    I have the following macro set up to create a folder in a directory as per the contents of 2 cells

    C2 = Folder name to be created
    B2= Date for previous working day


    Sub CreateFolderUsingRangeValue()
    Dim ObjA, NewFolder
    Const RootFolder As String = "\\ccfilesvr\shared\membership services\operations\supervisors\RESOURCE AREA stats & info\Statistics\Daily Stats Import 2014\Daily Stats July 2014"
    NewFolder = RootFolder & "\" & Sheets("Move & Rename").Range("C2").Value & Format(Sheets("Move & Rename").Range("B2 ").Value, " dd mmm yy")
    Set ObjA = CreateObject("scripting.filesystemobject")
    If ObjA.folderexists(NewFolder) = True Then
    If MsgBox("Folder Exists, do you want to erase it?", vbYesNo, "CriticalInfor...") = vbNo Then
    
    Exit Sub
    Else
    ObjA.deletefolder (NewFolder)
    ObjA.createfolder (NewFolder)
    End If
    Else
    ObjA.createfolder (NewFolder)
    End If
    End Sub
    I then have another macro that moves files into a specified folder within the same location ( I then manually move them into the folder the first macro created)

    Is there a way I could get the files to move to the newly created folder without me having to do it manually.

    I hope this makes sense

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Files to be moved to folder created by another macro

    You can use the filesystemobject to move files using either .move or .movefile

    .movefile is probably better as you can use it to do multiple files at once, i.e. use a wildcard as the sourcefile name. See below from the helpfile. Alternatively loop through the files and use .move for each one.

    MoveFile Method

    Description

    Moves one or more files from one location to another.

    Syntax

    object.MoveFile source, destination

    The MoveFile method syntax has these parts:

    Part Description
    object Required. Always the name of a FileSystemObject.
    source Required. The path to the file or files to be moved. The source argument string can contain wildcard characters in the last path component only.
    destination Required. The path where the file or files are to be moved. The destination argument can't contain wildcard characters.

    Remarks

    If source contains wildcards or destination ends with a path separator (\), it is assumed that destination specifies an existing folder in which to move the matching files. Otherwise, destination is assumed to be the name of a destination file to create. In either case, three things can happen when an individual file is moved:


    If destination does not exist, the file gets moved. This is the usual case.
    If destination is an existing file, an error occurs.
    If destination is a directory, an error occurs.

  3. #3
    Forum Contributor
    Join Date
    04-17-2012
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    122

    Re: Files to be moved to folder created by another macro

    This is what I already have set up to move the files

    Sub Move_AVHT_CC()
        
        Dim strFile As String, strNewFile As String
        
        Const strSourcePath As String = "A:\"
        Const strDestinationPath As String = "P:\membership services\operations\supervisors\RESOURCE AREA stats & info\Statistics\Daily Stats Import 2014\Daily Stats July 2014\"
        
        strNewFile = Sheets("Move & Rename").Range("A4").Value & " " & _
                     Format(Sheets("Move & Rename").Range("B2").Value, "dd mmm yy") & ".xls"
        
        If Dir$(strDestinationPath & strNewFile) <> "" Then
            MsgBox "There is already an existing file named " & vbLf & strDestinationPath & strNewFile, _
                   vbExclamation, "Dated File Name Exists"
            Exit Sub
        End If
        
        strFile = Dir$(strSourcePath & "AVHT CC*")
        If strFile <> "" Then
            Name (strSourcePath & strFile) As (strDestinationPath & strNewFile)
            
        Else
            MsgBox "No file found. ", , "File Not Found"
        End If
        
    End Sub
    Could this be tweaked to achieve what I want?

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Files to be moved to folder created by another macro

    Just change the destination path to whatever you create in the first macro? For example:
    Sub CreateFolderUsingRangeValue()
    Dim ObjA, NewFolder As String
    Const RootFolder As String = "\\ccfilesvr\shared\membership services\operations\supervisors\RESOURCE AREA stats & info\Statistics\Daily Stats Import 2014\Daily Stats July 2014"
    NewFolder = RootFolder & "\" & Sheets("Move & Rename").Range("C2").Value & Format(Sheets("Move & Rename").Range("B2 ").Value, " dd mmm yy")
    Set ObjA = CreateObject("scripting.filesystemobject")
    If ObjA.folderexists(NewFolder) = True Then
        If MsgBox("Folder Exists, do you want to erase it?", vbYesNo, "CriticalInfor...") = vbNo Then
            Exit Sub
        Else
            ObjA.deletefolder (NewFolder)
            ObjA.createfolder (NewFolder)
        End If
    Else
        ObjA.createfolder (NewFolder)
    End If
    Call Move_AVHT_CC(NewFolder)
    End Sub
    Sub Move_AVHT_CC(strDestinationPath As String)
    Dim strFile As String, strNewFile As String
    Const strSourcePath As String = "A:\"
    strNewFile = Sheets("Move & Rename").Range("A4").Value & " " & _
                 Format(Sheets("Move & Rename").Range("B2").Value, "dd mmm yy") & ".xls"
    If Dir$(strDestinationPath & strNewFile) <> "" Then
        MsgBox "There is already an existing file named " & vbLf & strDestinationPath & strNewFile, _
               vbExclamation, "Dated File Name Exists"
        Exit Sub
    End If
    strFile = Dir$(strSourcePath & "AVHT CC*")
    If strFile <> "" Then
        Name (strSourcePath & strFile) As (strDestinationPath & strNewFile)
    Else
        MsgBox "No file found. ", , "File Not Found"
    End If
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Files to be moved to folder created by another macro

    As ragulduy says.. .move or .movefile works well..

    snb has some nice examples here:

    http://www.snb-vba.eu/VBA_Bestanden_en.html#L_85

+ 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. [SOLVED] Need help opening a newly created folder and placing files into it
    By gleppelman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2013, 04:24 PM
  2. Macro to create a folder and then save all files in that folder
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2013, 01:49 PM
  3. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  4. Macro for copying specific data from excel files in a folder to a newly created excel file
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-06-2012, 02:24 PM
  5. Moving files folder to folder using macro.
    By azureriders in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-21-2011, 01:04 AM

Tags for this Thread

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