为什么VBA找不到指定文件夹中的文件

时间:2018-09-01 14:27:23

标签: vba

Sub ReformatForALFA()

    Filename = Dir("C:\Users\Daisy\Desktop\Cloudy\*.*")
    Do While Len(Filename) > 0

    Workbooks.Open (Filename)

    row_num = Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Rows.Count

    myRng = Empty
    For i = 1 To row_num
        If myRng = Empty And Cells(i, 1) Like "*value*row*" = False Then
            myRng = Rows(i).Address()
        ElseIf Cells(i, 1) Like "*value*row*" = False Then
            myRng = myRng & "," & Rows(i).Address()
        Else
            Pos1 = InStr(Cells(i, 1), "=") + 2
            Pos2 = InStr(Cells(i, 1), ">") - 1
            Pos3 = InStr(Cells(i, 1), "/") - 1
            Age = Mid(Cells(i, 1), Pos1, Pos2 - Pos1)
            Num = Mid(Cells(i, 1), Pos2 + 2, Pos3 - (Pos2 + 2))

            Cells(i, 2) = Age
            Cells(i, 3) = Num
        End If
    Next i

    Range(myRng).Delete
    Columns(1).EntireColumn.Delete

    Filename = Dir()
    Loop
End Sub

您好,我在上面编写了宏以遍历文件夹中的所有文本文件。该文件夹中有两个文本文件。当我运行它时,它指示我“由于找不到,已移动等原因,我们找不到Tuesday.txt。有人可以帮助我解决此问题。谢谢! 雏菊

1 个答案:

答案 0 :(得分:3)

Filename = Dir("C:\Users\Daisy\Desktop\Cloudy\*.*")

只会检索不带路径的文件名。您可以使用chdir更改路径,也可以将该路径添加到要打开的文件中

ChDir ("C:\Users\Daisy\Desktop\Cloudy")

Workbooks.open("C:\Users\Daisy\Desktop\Cloudy\" & filename)
相关问题