+ Reply to Thread
Results 1 to 7 of 7

How to add 2 features to a file transfer macro.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2019
    Location
    Seattle
    MS-Off Ver
    Office 2016
    Posts
    4

    How to add 2 features to a file transfer macro.

    I have the following Macro below and I wonder if anyone can help me modify it to do the following two things:
    • Move to the next line when there's and error. Sometimes when a file is being copied to a folder, someone else has a file with the same name open.
    • Create a new tab with a list of the lines that didn't run due to errors.

    Thank you



    Sub DRILL_XFER()
    '
    ' DRILL PL TRANSFER MACRO
    ' TRANSFER DRILLS AND PLS DAILY DURING CLOSE
    '
    FileCopy "H:\File1 - 2018-12-31.xls", "H:\File1 - 12-2018.xls"
    FileCopy "S:\File2.xls", "H:\File2 - 2018-12-31.xls"
    
    MsgBox "Drill & PL Transfer is Complete"
    
    End Sub
    Last edited by forkenbrock; 01-13-2019 at 04:36 AM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: How to add 2 features to a file transfer macro.

    1)
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    2) Untested.
    Sub DRILL_XFER()
    '
    ' DRILL PL TRANSFER MACRO
    ' TRANSFER DRILLS AND PLS DAILY DURING CLOSE
    '
        Dim e, msg As String
        For Each e In Array(Array("H:\File1 - 2018-12-31.xls", "H:\File1 - 12-2018.xls"), _
                            Array("S:\File2.xls", "H:\File2 - 2018-12-31.xls"))
            If Dir(e(0)) <> "" Then
                If Not IsFileOpen(CStr(e(0))) Then
                    FileCopy e(0), e(1)
                Else
                    msg = msg & vbLf & e(0) & " is in use"
                End If
            Else
                msg = msg & vbLf & e(0) & " is not found"
            End If
        Next
        If Len(msg) Then
            Sheets.Add.Cells(1).Resize(UBound(Split(msg, vbLf)) + 1).Value = _
            Application.Transpose(Split(msg, vbLf))
        Else
            MsgBox "Drill & PL Transfer is Complete"
        End If
    End Sub
    
    Function IsFileOpen(fName As String) As Boolean
        Dim ff As Integer, errNum As Integer
        If Dir(fName, 0) = "" Then
            IsFileOpen = False: Exit Function
        End If
        On Error Resume Next
        ff = FreeFile
        Open fName For Input Lock Read As #ff
        Close ff
        errNum = Err
        On Error GoTo 0
        IsFileOpen = (errNum <> 0)
    End Function

  3. #3
    Registered User
    Join Date
    01-12-2019
    Location
    Seattle
    MS-Off Ver
    Office 2016
    Posts
    4

    Re: How to add 2 features to a file transfer macro.

    Thank you jindon, this is awesome. I've been testing on my C: drive. The report transfers the files, but gives an error when a copy of the file being saved is already open. It says "Run-time Error '70': Permission denied". In debug it stops at "FileCopy e(0), e(1)".

    Sub DRILL_XFER()
    '
    ' DRILL PL TRANSFER MACRO
    ' TRANSFER DRILLS AND PLS DAILY DURING CLOSE
    '
        Dim e, msg As String
        For Each e In Array(Array("C:\Test_A\29DVS - 2018-12-31.xls", "C:\Test_B\29DVS - 12-2018.xls"), _
        Array("C:\Test_C\US WEST REGIONCOMP.xls", "C:\Test_D\US WEST REGIONCOMP - 2018-12-31.xls"))
    
            If Dir(e(0)) <> "" Then
                If Not IsFileOpen(CStr(e(0))) Then
                    FileCopy e(0), e(1)
                Else
                    msg = msg & vbLf & e(0) & " is in use"
                End If
            Else
                msg = msg & vbLf & e(0) & " is not found"
            End If
        Next
        If Len(msg) Then
            Sheets.Add.Cells(1).Resize(UBound(Split(msg, vbLf)) + 1).Value = _
            Application.Transpose(Split(msg, vbLf))
        Else
    
    MsgBox "Drill & PL Transfer is Complete"
        End If
    
    End Sub
    
    Function IsFileOpen(fName As String) As Boolean
        Dim ff As Integer, errNum As Integer
        If Dir(fName, 0) = "" Then
            IsFileOpen = False: Exit Function
        End If
        On Error Resume Next
        ff = FreeFile
        Open fName For Input Lock Read As #ff
        Close ff
        errNum = Err
        On Error GoTo 0
        IsFileOpen = (errNum <> 0)
    End Function

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: How to add 2 features to a file transfer macro.

    Not sure why, but see what happens.
    Sub DRILL_XFER()
    '
    ' DRILL PL TRANSFER MACRO
    ' TRANSFER DRILLS AND PLS DAILY DURING CLOSE
    '
        Dim e, msg As String, fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        For Each e In Array(Array("C:\Test_A\29DVS - 2018-12-31.xls", "C:\Test_B\29DVS - 12-2018.xls"), _
                Array("C:\Test_C\US WEST REGIONCOMP.xls", "C:\Test_D\US WEST REGIONCOMP - 2018-12-31.xls"))
            If fso.FileExists(e(0)) Then
                If Not IsFileOpen(CStr(e(0))) Then
                    fso.CopyFile e(0), e(1), True
                Else
                    msg = msg & vbLf & e(0) & " is in use"
                End If
            Else
                msg = msg & vbLf & e(0) & " is not found"
            End If
        Next
        If Len(msg) Then
            Sheets.Add.Cells(1).Resize(UBound(Split(msg, vbLf)) + 1).Value = _
            Application.Transpose(Split(msg, vbLf))
        Else
    
    MsgBox "Drill & PL Transfer is Complete"
        End If
    
    End Sub
    
    Function IsFileOpen(fName As String) As Boolean
        Dim ff As Integer, errNum As Integer
        On Error Resume Next
        ff = FreeFile
        Open fName For Input Lock Read As #ff
        Close ff
        errNum = Err
        On Error GoTo 0
        IsFileOpen = (errNum <> 0)
    End Function

  5. #5
    Registered User
    Join Date
    01-12-2019
    Location
    Seattle
    MS-Off Ver
    Office 2016
    Posts
    4

    Re: How to add 2 features to a file transfer macro.

    Hello jindon. It seems to give the same error and stop at about the same place.

    fso.CopyFile e(0), e(1), True

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: How to add 2 features to a file transfer macro.

    What are the both value for e(0) and e(1) when you get into a debug mode?

    You can see when you hover over the cursor (not click) the variable.

  7. #7
    Registered User
    Join Date
    01-12-2019
    Location
    Seattle
    MS-Off Ver
    Office 2016
    Posts
    4

    Re: How to add 2 features to a file transfer macro.

    This is what it says:

    e(0) = "C:\Test_C\US WEST REGIONCOMP.xls"
    e(1) = "C:\Test_D\US WEST REGIONCOMP - 2018-12-31.xls"

    In this case the destination file in folder D was the one that was open. If I open the file in destination folder B, then e(0) refers to folder A and e(1) refers to folder B.
    Last edited by forkenbrock; 01-17-2019 at 06:13 PM.

+ 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] Macro code to transfer data to another file
    By RA_Lek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-04-2015, 08:09 PM
  2. Macro to transfer data from multiple workbooks into separate sheets of master file
    By salva7ore in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2014, 08:18 PM
  3. Macro to Open a file, transfer data, save and close
    By tforbes75 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2011, 11:59 AM
  4. Userform to Control Macro Features. Macro = Search for File Type
    By R_S_6 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 12-13-2010, 11:51 AM
  5. macro to transfer data from one file to another
    By sha_ch in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2010, 12:31 AM
  6. filter features+macro
    By excellentexcel in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 01-09-2009, 10:31 AM

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