【问题标题】:VBA loop throught each cell to copy filesVBA 循环遍历每个单元格以复制文件
【发布时间】:2016-10-07 02:17:07
【问题描述】:

我有一个 Excel 文件,其中列 (icol) 每个单元格都包含一些文件的路径,如下所示:

             column A                         column B          column c
P:\Desktop\Source\Test1-folder\file1.txt       empty column     P:\Desktop\Source\Test1-folder\filetest.txt            
P:\Desktop\Source\Test1-folder\file2.txt        .....

我需要遍历这些单元格以将单元格中的文件复制到目标文件夹中,但我无法成功。谁能帮忙怎么做?

Dim strSlash As String, destinationFolder As String
Dim lastcolumn As Long, icol As Long, lastLigne As Long
Dim rngCell As Range, rngFiles As Range
Dim FSO As New FileSystemObject
destinationFolder = "P:\Desktop\folderdestination"
Dim maListe As Object
Dim workboo As Workbook
Dim worksh As Worksheet

Set workboo = Workbooks.Open(P:\Desktop\Source\excelfile.xlsx)
Set worksh = workboo.Worksheets("path_files")

lastcolumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
     If Dir(destinationFolder, 16) = "" Then MkDir (destinationFolder)
         For icol = 1 To lastcolumn Step 2
            lastLigne = Cells(Rows.Count, icol).End(xlUp).Row
            Set rngFiles = Cells(1, icol).Resize(lastLigne)
                 For Each rngCell In rngFiles.Cells
                 If Dir(rngCell.Value) <> "" Then 
                 strFile = Right(rngCell.Value, Len(rngCell.Value) - InStrRev(rngCell.Value, "\"))
                 If Dir(destinationFolder & "\" & Left(strFile, 5) , 16) = "" Then
                  FSO.CopyFile rngCell.Value, destinationFolder & "\" & Left(strFile, 5)           
                   End If
                 End If
                  Next rngCell
          Next icol

结束子

【问题讨论】:

  • 那里有一大堆语法错误。 destinationFolder 需要在引号内,您从未声明或设置 dercol 任何内容,您似乎在使用 FSO 对象而没有创建或设置它,您缺少第一个 For 循环的 Next .. . 你确定发布了所有的代码吗?好像少了一点……
  • @Dave 我编辑它,因为我一开始只放了一部分代码
  • 好的。首先,将destination folder的声明更正为destinationFolder,变量中不能有空格。然后将worksh 的任何实例替换为ws,因为这就是您声明并设置为工作表的内容。我仍然没有看到 dercol(在For k = 1 To dercol 行中)在任何地方定义,因此它将初始化为零并阻止循环执行。您仍然缺少 Next,因为您有两个 For 循环,每个循环都需要自己的 Next...
  • 并将 Option Explicit 放在页面的最顶部;这将告诉 VBA 确保您已声明所有正在使用的变量。
  • 我只放了一部分代码,因为我的程序下面还有代码。但主要问题是它不会复制任何这些文件@Dave

标签: vba excel


【解决方案1】:

已编辑以添加对源文件存在的检查

应该这样做

Option Explicit

Sub main()

    Dim strSlash As String, destinationFolder As String
    Dim lastcolumn As Long, icol As Long, lastLigne As Long
    Dim rngCell As Range, rngFiles As Range
    Dim FSO As New FileSystemObject

    strSlash = "\"
    destinationFolder = "P:\Desktop\folderdestination"
    lastcolumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    For icol = 1 To lastcolumn Step 2
        lastLigne = Cells(Rows.Count, icol).End(xlUp).Row
        Set rngFiles = Cells(1, icol).Resize(lastLigne)
        For Each rngCell In rngFiles.Cells
            If Dir(rngCell.Value) <> "" Then '<~~ check if the source file is actually there!
                If Dir(destinationFolder & "\" & Right(rngCell.Value, Len(rngCell.Value) - InStrRev(rngCell.Value, strSlash)), 16) = "" Then
                    FSO.CopyFile rngCell.Value, destinationFolder & "\" & Right(rngCell.Value, Len(rngCell.Value) - InStrRev(rngCell.Value, strSlash))
                End If
            End If
        Next rngCell
    Next icol

End Sub

但它仍然可以在很大程度上改进,更彻底地利用 FileSystemObject(这当然需要添加对“Microsoft Scripting Runtime”库的引用:Tools->References 然后向下滚动列表框并选择“Microsoft Scripting Runtime”复选框)

【讨论】:

  • 谢谢!但最后一件事,它不会复制 C 列中的文件,它只对 A 列执行,尽管它不是最后一列
  • 循环遍历索引不均匀的列(即 1、3、5,...),从列“A”(索引 1)到第 1 行中非空单元格的最后一个。从你的问题我假设lastcolumn 返回2,好像列“C”在第一行没有任何值:是这样吗?检查lastcolumn返回值
  • 不,问题是它返回 3 ,但它不复制 c 列中的文件,这很奇怪
  • 单步执行代码并使用即时窗口查询变量值。例如,一旦它开始第二次iCol 迭代,检查lastLigne 设置的值。它应该是一个“有效”的(比如:5)然后进入For Each rngCell 循环并检查每个rngCell 对象的Address 和Value 属性。如果仍然可以,则检查 Dir(destinationFolder ... 返回值。很可能在这里您会发现问题,因为您使用带有16“属性”的Dir() 函数代表vbDirectory 即:“除了没有属性的文件之外,还指定目录或文件夹”
  • 但我的代码中有一个错误:我没有像第二次那样将(Len(rngCell.Value) - InStrRev(rngCell.Value, strSlash)) 的第一次出现更改为Right(rngCell.Value, Len(rngCell.Value) - InStrRev(rngCell.Value, strSlash))(紧随其后!)。查看编辑的代码
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多