【问题标题】:excel vba move each text file to a new directory using the file name?excel vba 使用文件名将每个文本文件移动到新目录?
【发布时间】:2015-05-27 09:40:02
【问题描述】:

我正在使用以下 vba 代码将我的所有文本文件导入到 excel 中的新行中。这一点工作正常,接下来我要做的是一旦导入了文本文件,我希望将每个文本文件从一个目录 'Z:\NS\Unactioned\' 移动到另一个名为 Actioned 'Z:\NS\Actioned\&Filename\' 的目录。

然后在该文件夹中,根据文件名(减去文件扩展名)为每个文本文件创建一个文件夹,然后我可以将每个文本文件放在相应的文件夹中。

所以如果我的文件夹中有 3 个 .txt 文件 Unactioned:

1.txt
2.txt
3.txt

然后每个 txt 文件将像这样移动:

Actioned/1/1.txt
Actioned/2/2.txt
Actioned/3/3.txt

有人可以告诉我如何做到这一点吗?谢谢

代码:

Sub Import_All_Text_Files_2007()

    Dim nxt_row As Long

     'Change Path
    Const strPath As String = "Z:\NS\Unactioned\"
    Dim strExtension As String

     'Stop Screen Flickering
    Application.ScreenUpdating = False

    ChDir strPath

     'Change extension
    strExtension = Dir(strPath & "*.txt")

    Do While strExtension <> ""


         'Sets Row Number for Data to Begin
        If Range("C1").Value = "" Then
  nxt_row = 1
Else
  If Range("C2").Value = "" Then
    nxt_row = 2
  Else
     nxt_row = Range("C1").End(xlDown).Offset(1).Row
  End If
End If

         'Below is from a recorded macro importing a text file
        FileNum = FreeFile()
curCol = 3
Open strPath & strExtension For Input As #FileNum
While Not EOF(FileNum)
    Line Input #FileNum, DataLine
    ActiveSheet.Cells(nxt_row, curCol) = DataLine
    curCol = curCol + 1
Wend
Close #FileNum

        strExtension = Dir
    Loop




    Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String
srcPath = "Z:\NS\Unactioned\"
destPath = "Z:\NS\Actioned\" & srcFile & "\"
ext = Array("*.txt", "*.xls")
For Each x In ext
    d = Dir(srcPath & x)
        Do While d <> ""
            srcFile = srcPath & d
            FileCopy srcFile, destPath & d
            Kill srcFile
            d = Dir
        Loop
Next


    Application.ScreenUpdating = True



End Sub

【问题讨论】:

    标签: vba excel move


    【解决方案1】:

    您放错了destPath,所以它没有填写文档名称。 忘记创建目标目录(使用MKDir)和最后一个d=Dir 语句的参数

    试试这个(对我有用):

    Sub Import_All_Text_Files_2007()
    Dim d As String, ext, x
    Dim srcPath As String, destPath As String, srcFile As String
    Dim strExtension As String
    Dim nxt_row As Long
    
    'Change Path
    Const strPath As String = "Z:\NS\Unactioned\"
    
    'Stop Screen Flickering
    Application.ScreenUpdating = False
    
    ChDir strPath
    
    'Change extension
    strExtension = Dir(strPath & "*.txt")
    
    Do While strExtension <> ""
        'Sets Row Number for Data to Begin
        If Range("C" & Rows.Count).End(xlUp).Offset(1).Row >= 5 Then
            nxt_row = Range("C" & Rows.Count).End(xlUp).Offset(1).Row
        Else
            nxt_row = 5
        End If
    
        'Below is from a recorded macro importing a text file
        FileNum = FreeFile()
        curCol = 3
        Open strPath & strExtension For Input As #FileNum
        While Not EOF(FileNum)
            Line Input #FileNum, DataLine
            ActiveSheet.Cells(nxt_row, curCol) = DataLine
            curCol = curCol + 1
        Wend
        Close #FileNum
    
        strExtension = Dir
    Loop
    
    
    srcPath = "Z:\NS\Unactioned\"
    ext = Array("*.txt", "*.xls")
    
    For Each x In ext
        d = Dir(srcPath & x)
        Do While d <> ""
            srcFile = srcPath & d
            destPath = "Z:\NS\Actioned\" & Left(d, Len(d) - 4) & "\"
            If Dir(destPath, 16) = "" Then MkDir (destPath)
            FileCopy srcFile, destPath & d
            Kill srcFile
            d = Dir(srcPath & x)
        Loop
    Next x
    
    Application.ScreenUpdating = True
    
    End Sub
    

    【讨论】:

    • 谢谢这很好用,但还有一件事你知道我怎样才能在第 5 行之后开始输入数据吗?
    • 测试编辑。需要更改的是nxt_row 的定义!
    • 感谢我尝试了编辑,但我在此行收到应用程序或对象未定义错误:如果 Range("C1").End(xlDown).Offset(1).Row >= 5 Then
    • Mkay 我保留了您的代码,但如果工作表为空,则 End(xlDown) 将位于最后一行并且无法偏移更多。查看编辑;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多