从excel宏

时间:2016-03-18 08:22:47

标签: excel vba excel-vba

我想要一个帮助将整个文件夹从一个路径移动到另一个路径,我的要求是:

我有A2A51列中的路径列表(不同路径)作为源位置,列B2B51目的地位置,我想要移动列A1文件夹到B1目的地位置。

A中的

列表中列出了不同的100个路径,“B”列中有一个目标路径,应该移动文件夹。

2 个答案:

答案 0 :(得分:1)

您需要创建一个循环遍历A列的vba宏。How to loop through a range

然后应该从它旁边的列中获取值&用它来调用Copy Folder method

答案 1 :(得分:0)

您正在寻找移动文件夹,因此这里有一些将原点移动到目标中的代码。

Sub MoveModules()

  'Add a Reference to Microsoft Scripting Runtime

  'Column A should have a path like C:\test\AAA
  'Column B should have a path like D:\test\AAA

  'The move would MOVE C:\test\AAA to D:\test\AAA

  Dim rng As Range
  Dim aData As Variant
  Dim FSO As Scripting.FileSystemObject
  Dim iCounter As Long
  Dim sOrigin As String
  Dim sTarget As String

  'Get the intersection of columns A and B and the usedrange
  Set rng = Range("A:B " & Sheet1.UsedRange.Address)

  If Not rng Is Nothing Then

    aData = rng
    Set FSO = New FileSystemObject

    For iCounter = LBound(aData) To UBound(aData)
      sOrigin = aData(iCounter, 1)
      sTarget = aData(iCounter, 2)
      If FSO.FolderExists(sOrigin) Then
        If Not FSO.FolderExists(sOrigin) Then
          FSO.MoveFolder sOrigin, sTarget
        Else
          MsgBox "Target folder already exists at '" & sTarget & "'"
        End If
      Else
        MsgBox "Can't find Source folder '" & sOrigin & "'"
      End If
    Next iCounter
  End If
End Sub