无法在其他PC上运行VBA宏

时间:2013-09-09 08:53:34

标签: excel vba path directory

我正在寻找部分VBA代码的帮助。它在几台PC上运行没有任何问题,但是当它涉及到它时根本不起作用。没有错误返回。如果有人看到可能出错的地方,请告诉我。

fn = Dir(ThisWorkbook.Path & "\*.xl*")
Do While ((fn <> "") And (Not (fn Like "*Inside*")))

    Range("B4").value = fn

    Call setCorrectValues

    fn = Dir()
Loop

1 个答案:

答案 0 :(得分:0)

最近与该死的Dir功能陷入了麻烦。试试这个:

Dim objFSO As Variant
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim FolderName As String
FolderName = ThisWorkbook.Path

Dim objFolder As Variant
Set objFolder = objFSO.GetFolder(FolderName)
Debug.Print objFolder.Path

Dim objFiles As Variant
Set objFiles = objFolder.Files
Dim objFile As Variant
For Each objFile In objFiles
    Debug.Print objFile.Name
    If Not (fn Like "*Inside*.xls") Then
        Range("B4").Value = fn
        Call setCorrectValues
        Exit For
    End If
Next