错误的文件名或编号vba

时间:2018-08-21 20:25:47

标签: excel-vba

我的代码不断出现错误的文件名或数字错误,而且我不知道问题出在哪里,我们将不胜感激!我正在尝试将基于用户选择的文件路径存储为变量,稍后可以在vlookup中引用。以下是我的代码,我无法弄清楚出了什么问题,但是我在另一个编译良好的宏中使用了粘贴的代码。

sub edits
dim xpath and xfile as string
xPath = NewPath 'Newpath function executes
xfile = Dir$(xPath & "*.xlsm*", vbNormal) 'error here 
Set SourceBook = Workbooks.Open(xPath & xfile)
End Sub

Function NewPath() As String
    With Application.FileDialog(msoFileDialogOpen)
        .ButtonName = "Choose a file"
        .Title = "Previous File"
        .AllowMultiSelect = False
        If .Show Then xPath = .SelectedItems(1) & "\"
    End With
End Function

下面是我使用的已编译的代码,它使用户选择一个文件夹而不是一个文件

sub something
dim xpath and xfile as string
xPath = NewPath
If Not strPath = vbNullString Then
xfile = Dir$(xPath & "*.xlsm", vbNormal)
Do While Not xfile = vbNullString
    'some code
    Set SourceBook = Workbooks.Open(xPath & xfile)
    SourceBook.Close False
    xfile = Dir$()
    Loop
End If
End Sub

Function NewPath() As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .ButtonName = "Choose a folder"
        .Title = "Folder Picker"
        .AllowMultiSelect = False
        If .Show Then NewPath = .SelectedItems(1) & "\"
    End With
 End Function

0 个答案:

没有答案