【问题标题】:MOVE FILE IN VBA在 VBA 中移动文件
【发布时间】:2022-01-15 15:25:49
【问题描述】:

我希望下面的代码使用 2 种方法。第一种方法是复制文件,第二种方法是移动文件。对于方法 1,我做了评论,以便执行方法 2,但它不起作用,出现错误。

Sub movecopyFiles()
  Dim sh As Worksheet, lastR As Long, arrA, i As Long, k As Long
  Dim fileD As FileDialog, strDestFold As String, FSO As Object
  
  Set sh = ActiveSheet
  lastR = sh.Range("A" & sh.Rows.Count).End(xlUp).Row ' last row on A:A column
  arrA = sh.Range("A2:E" & lastR).Value2                   'place the range in an array for faster iteration
  Set FSO = CreateObject("Scripting.FileSystemObject")
  With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select the destination folder!"
        .AllowMultiSelect = False
        If .Show = -1 Then
            strDestFold = .SelectedItems.Item(1) & "\"   'select the destination folder
        End If
  End With
  If strDestFold = "" Then Exit Sub                         'in case of  not selecting any folder
  For i = 1 To UBound(arrA)
     If UCase(arrA(i, 5)) = "V" Then                         'copy the file only if a "V" exists in column E:E
        If FSO.FileExists(arrA(i, 1)) Then                    'check if the path in excel is correct
'            FSO.COPYFILE arrA(i, 1), strDestFold, True     'copy the file (True, to overwrite the file if it exists)
            FSO.moveFILE arrA(i, 1), strDestFold, True     'move the file (True, to overwrite the file if it exists) >> error this line 
            k = k + 1
        Else
            MsgBox arrA(i, 1) & " file could not be found." & vbCrLf & _
                        "Please, check the spelling and correct the file full path!", vbInformation, _
                        "File does not exist..."
        End If
     End If
  Next i
  MsgBox "Copied " & k & " files in " & strDestFold, , "Ready..."
End Sub

【问题讨论】:

  • 据我记忆,这个方法只有两个参数。所以,它应该变成FSO.moveFILE arrA(i, 1).value, strDestFold
  • 如果你更喜欢MoveFile,但你想覆盖,你不能buildforce方法中不存在的参数。您可以使用FSO.COPYFILE,后跟FSO.DeleteFile arrA(i, 1).value。还有一些其他的方法。或者在移动之前检查文件是否存在并Kill...
  • @FaneDuru ,FSO.moveFILE arrA(i, 1).value, strDestFold 我使用它,但值的答案,即未定义的变量。我只想使用copyfile的前2个方法,如果我想使用move file,我只需要注释copyfile代码的代码。我想你可以以你的答案的形式给我。如果可能,您创建的代码结构永远不会改变。我需要评论或取消评论,或者如果你有最好的建议
  • Ups...哦,不,我错了,我把数组当成一个范围。请删除value。在第二种情况下,也是...... :)
  • @FaneDuru , FSO.moveFILE arrA(i, 1), strDestFold 如果我使用代码它可以工作,但如果目标文件夹中有相同的文件,则会出现“文件已存在”错误。如果我一直强制移动文件有什么解决办法?

标签: vba fso movefile


【解决方案1】:

请以这种方式调整以下部分:

 Dim fileName As String  'new variable to be declared
 'your existing code...
      If FSO.FileExists(arrA(i, 1)) Then                       'check if the path in excel is correct
                fileName = Right(arrA(i, 1), Len(arrA(i, 1)) - InStrRev(arrA(i, 1), "\"))
                If FSO.FileExists(strDestFold & fileName) Then Kill strDestFold & fileName 'delete file if exists
                FSO.MoveFile arrA(i, 1), strDestFold           'move the file 
                k = k + 1
        Else
                MsgBox arrA(i, 1) & " file could not be found." & vbCrLf & _
                            "Please, check the spelling and correct the file full path!", vbInformation, _
                            "File does not exist..."
        End If
  'your existing code

【讨论】:

  • @roy 还有什么不清楚的地方吗?
  • @roy 您没有抽出时间检查上述建议吗?如果经过测试,它没有返回您需要的内容吗?
  • 一切顺利。非常感谢
最近更新 更多