+ Reply to Thread
Results 1 to 18 of 18

Move/Copy macro doesn't work as aspected

Hybrid View

  1. #1
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Move/Copy macro doesn't work as aspected

    Hi.

    Got a code that will save all selected files in a directory chosen by the user.
    Often it works great but sometimes it doesn't copy/move all the selected files, just a few (about 5-6) and sometimes the new files are just 1kb and doesn't work like the program only copied the icon.

    Anyone knows a diffrent way of writing this code ? A code that doesn't bugg ?


    My Code:

    Sub MoveFiles()
        MoveOrCopy Selection.Cells, False
    End Sub
    Sub CopyFiles()
        MoveOrCopy Selection.Cells, True
    End Sub
    Private Sub MoveOrCopy(r As Range, bCopy As Boolean)
       
       
        Dim cell        As Range
        Dim sDir        As String
        Dim sFile       As String
        Dim nFile       As Long
    
    
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Välj mapp"
            .AllowMultiSelect = False
            If .Show = 0 Then Exit Sub
            sDir = .SelectedItems(1) & "\"
        End With
    
        For Each cell In Selection.Cells
            If Len(cell.Text) Then
                sFile = Mid(cell.Text, InStrRev(cell.Text, "\") + 1)
    
                If Len(Dir(cell.Text)) Then
                    If bCopy Then
                        FileCopy cell.Text, sDir & sFile
                    Else
                        Name cell.Text As sDir & sFile
                    End If
                    
                    nFile = nFile + 1
            
                Else
                    MsgBox "Går inte att flytta: " & cell.Text
                End If
            End If
        Next cell
        
        
        MsgBox IIf(bCopy, "Kopierat ", "Flyttat ") & nFile & " fil(er)"
    
        
    End Sub
    Last edited by Pero; 09-25-2009 at 02:49 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Hi pero
    which line fails?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Move/Copy macro doesn't work as aspected

    sorry for slow reply !

    No line failes in the code when I run it.
    Sometimes it works and sometime it doesn't. And when you copy alot of files some of them is only 1Kb in the target folder.

    Weird..

    Just asking if its a way to write a more "Safe" code like this one.

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    The only thing I can see if the sfile name or sdir string are wrong in the range
    do you have a small sample workbook?

  5. #5
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Move/Copy macro doesn't work as aspected

    Sure!

    here is a dummybook.
    Attached Files Attached Files

  6. #6
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    I use this code to copy file(s)
    Sub CopyFile()
        Dim fs As Object
        Dim strFile As String
        Dim strNewFile As String
        strFile = "C:\Ptest.xls"
        strNewFile = "C:\Program Files\Ptest.xls"
        Set fs = CreateObject("Scripting.FileSystemObject")
        fs.CopyFile strFile, strNewFile
        MsgBox "A copy of the specified file was created."
        Set fs = Nothing
    End Sub
    or you may need to check if
    Sub DoesFolderExist()
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        MsgBox fs.FolderExists("C:\Program Files")
    End Sub
    
    Sub FileExists()
        Dim fs As Object
        Dim strFile As String
        Set fs = CreateObject("Scripting.FileSystemObject")
        strFile = InputBox("Enter the full name of the file:")
        If fs.FileExists(strFile) Then
            MsgBox strFile & " was found."
        Else
            MsgBox "File does not exist."
        End If
    End Sub
    I check your code out when I get a chance
    See ya

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    ooOps

    Wrong post
    Last edited by pike; 09-17-2009 at 05:23 PM. Reason: Wrong post

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Hi Pero,
    try this..half finnished code ....
    Option Explicit
    Sub MoveFiles()
       MoveOrCopy Selection.Cells, False
    End Sub
    Sub CopyFiles()
        MoveOrCopy Selection.Cells, True
    End Sub
    Sub MoveOrCopy(r As Range, bCopy As Boolean)
    Dim z!, e!, a, u$, sDir$, fs As Object
    On Error Resume Next
    With Selection.Cells
    a = .Cells
    z = .Rows.Count
    End With
    If z = 0 Then
    MsgBox "select some cells"
    Exit Sub
    End If
     With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Välj mapp"
            .AllowMultiSelect = False
            If .Show = 0 Then Exit Sub
            sDir = .SelectedItems(1) & "\"
    End With
    For e = 1 To z
    u = a(e, 2) & "\" & a(e, 1) & ".*"
    Set fs = CreateObject("scripting.filesystemobject")
             If bCopy = True Then
                fs.Copyfile u, sDir
              Else: fs.Movefile u, sDir
            End If
    Set fs = Nothing
    Next
     Exit Sub
    Errorhandler:
        If Err = "75" Then
            MsgBox ("Åtkomst nekad. Kan bero på att filen är öppen eller att du inte har behörighet till filen.")
        End If
        If Err = "70" Then
            MsgBox ("Du kan inte kopiera en öppen fil. Stäng filen och försök igen.")
            Exit Sub
        End If
    End Sub
    will have to add these checks
    Sub DoesFolderExist()
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        MsgBox fs.FolderExists("C:\Program Files")
    End Sub
    
    Sub FileExists()
        Dim fs As Object
        Dim strFile As String
        Set fs = CreateObject("Scripting.FileSystemObject")
        strFile = InputBox("Enter the full name of the file:")
        If fs.FileExists(strFile) Then
            MsgBox strFile & " was found."
        Else
            MsgBox "File does not exist."
        End If
    End Sub
    Last edited by pike; 09-18-2009 at 07:19 AM. Reason: code added line

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    this should have it
    cant find if the file exist with out the extension
    Option Explicit
    Sub MoveFiles()
       MoveOrCopy Selection.Cells, False
    End Sub
    Sub CopyFiles()
        MoveOrCopy Selection.Cells, True
    End Sub
    Sub MoveOrCopy(r As Range, bCopy As Boolean)
    Dim z!, e!, a, u$, sDir$, fs As Object
    With Selection.Cells
    a = .Cells
    z = .Rows.Count
    End With
    If z = 0 Then
    MsgBox "select some cells"
    Exit Sub
    End If
     With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Välj mapp"
            .AllowMultiSelect = False
            If .Show = 0 Then Exit Sub
            sDir = .SelectedItems(1) & "\"
    End With
    For e = 1 To z
    u = a(e, 2) & "\" & a(e, 1) & ".*"
    If DoesFolderExist(a(e, 2) & "\") Then
    Set fs = CreateObject("scripting.filesystemobject")
             If bCopy = True Then
              On Error GoTo NoFile
                fs.Copyfile u, sDir
              ElseIf bCopy = False Then
                On Error GoTo NoFile
                 fs.Movefile u, sDir
              Else:
    NoFile:         MsgBox "File dosent exist : " & a(e, 1)
              End If
    Set fs = Nothing
    Else: MsgBox "Folder dosent exist : " & a(e, 2) & "\"
    End If
    Next
    End Sub
    Function DoesFolderExist(strFile) As Boolean
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
       If fs.FolderExists(strFile) Then
              DoesFolderExist = True
        Else
            DoesFolderExist = False
        End If
      End Function

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Pero how did the code go???

  11. #11
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Bump Bump Bump

  12. #12
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Move/Copy macro doesn't work as aspected

    Hey!

    Have been away for a while. Will test the code now and come back with the results.
    Thaks for your time!!

    EDIT:
    WHen I run the macros I get an error. Error nr 9, "The index is out of the interval"

    I got the errormessage in swedish and thne tried to translate it. Its not a quote.

    The line markedin VBA is:
    u = a(e, 2) & "\" & a(e, 1) & ".*"
    How do I fix this ?

    Again Thank you !

    //

    Per
    Last edited by Pero; 09-24-2009 at 01:03 PM.

  13. #13
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Pero did you select/highlight the files/folders to move or copy?

  14. #14
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Pero use..
    Sub MoveOrCopy(r As Range, bCopy As Boolean)
    Dim z!, e!, a, u, sDir$, fs As Object
    With Selection.Cells
    a = .Cells
    z = .Rows.Count
    
    If z < 2 Then
    MsgBox "select some cells"
    Exit Sub
    End If
    End With
     With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Välj mapp"
            .AllowMultiSelect = False
            If .Show = 0 Then Exit Sub
            sDir = .SelectedItems(1) & "\"
    End With
    For e = 1 To z
    u = a(e, 2) & "\" & a(e, 1) & "*"
    If DoesFolderExist(a(e, 2) & "\") Then
    Set fs = CreateObject("scripting.filesystemobject")
             If bCopy = True Then
              On Error GoTo NoFile
                fs.Copyfile u, sDir
              ElseIf bCopy = False Then
                On Error GoTo NoFile
                 fs.Movefile u, sDir
              Else:
    NoFile:         MsgBox "File dosent exist : " & a(e, 1)
              End If
    Set fs = Nothing
    Else: MsgBox "Folder dosent exist : " & a(e, 2) & "\"
    End If
    Next
    End Sub

  15. #15
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Move/Copy macro doesn't work as aspected

    Thank you again =)

    I'm very greatful for all of your help.
    The code is working now as it should.

    Just a few modifications need to be done.

    Everyting is explained in my DummyBok.



    //

    Per
    Attached Files Attached Files

  16. #16
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    Pero try..
    Option Explicit
    Sub MoveFiles()
        MoveOrCopy Selection.Cells, False
    End Sub
    Sub CopyFiles()
        MoveOrCopy Selection.Cells, True
    End Sub
    Sub MoveOrCopy(r As Range, bCopy As Boolean)
        Dim i!, ii!, e, a$, z$, sDir$, fs As Object
        i = r.Rows.Count
        ii = r.Columns.Count
        If i < 0 Then
            MsgBox "select some cells"
            Exit Sub
            elseIf ii > 1 Then
                MsgBox "selected too many Columns"
                Exit Sub
            End If
            With Application.FileDialog(msoFileDialogFolderPicker)
                .Title = "Välj mapp"
                .AllowMultiSelect = False
                If .Show = 0 Then Exit Sub
                sDir = .SelectedItems(1) & "\"
            End With
            For Each e In r
                a = e.Offset(0, 1).Value
                z = FileName(a)
                If DoesFolderExist(z) Then
                    If DoesFileExist(a) Then
                        Set fs = CreateObject("scripting.filesystemobject")
                        If bCopy = True Then
                            fs.Copyfile a, sDir
                        Else: bCopy = False
                            fs.Movefile a, sDir
                        End If
                    Else: MsgBox "File dosent exist : " & a
                    End If
                Else: MsgBox "Folder dosent exist : " & z
                End If
            Next
            Set fs = Nothing
        End Sub
    Function DoesFolderExist(strFile$) As Boolean
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        If fs.FolderExists(strFile) Then
            DoesFolderExist = True
        Else
            DoesFolderExist = False
        End If
    End Function
    Function DoesFileExist(strFile$) As Boolean
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        If fs.FileExists(strFile) Then
            DoesFileExist = True
        Else
            DoesFileExist = False
        End If
    End Function
    Function FileName(strFilename$) As String
        Dim intLastSep!
        intLastSep = InStrRev(strFilename, "\")
        FileName = Left(strFilename, intLastSep - 1)
    End Function


    Comment ; This code will differ from the email as I have to change the if to elseif
    Last edited by pike; 09-25-2009 at 04:24 AM. Reason: Comment

  17. #17
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Move/Copy macro doesn't work as aspected

    Everything is working great =)


    Thank you !!!

  18. #18
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Move/Copy macro doesn't work as aspected

    no thank you for marking the post solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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