Excel VBA无法在文件夹中找到xlsx文件

时间:2017-11-08 12:44:53

标签: excel vba excel-vba

嗨,我有一个代码,它会弹出一个按摩盒

  

运行时错误1004

它无法找到我要打开的文件。我该怎么处理?我已经陷入困境几个小时了,我找不到出了什么问题。我想打开一个与宏文件保存在同一文件夹中的XLSX文件,它们是文件夹中的唯一文件,有人可以帮忙吗?

Private Sub cmdStartMonth_Click()
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
'Analyze month by selecting
Dim myPath As String
Dim FileName As String
Dim UnionWB As Workbook
Dim MonthName As String
MonthName = ListMonth.Value
myExtension = "*.xlsx*"

    Dim sWrkbkPath As String
    sWrkbkPath = Dir$(ThisWorkbook.Path & "\*.xlsx")

    'Only expecting a single file so no need to loop.
    If sWrkbkPath <> "" Then
        Set UnionWB = Workbooks.Open(sWrkbkPath)
    End If

UnionWB.Worksheets("Union").Range("A1:Y1").AutoFilter
With UnionWB.Worksheets("Union")
        .Activate
        .Range("L1:W1").Find(MonthName, , xlValues, xlWhole).Activate
End With

Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

这是代码中有问题的行:

  Set UnionWB = Workbooks.Open(sWrkbkPath)

1 个答案:

答案 0 :(得分:2)

更改此

move_uploaded_file()

到这个

Set UnionWB = Workbooks.Open(sWrkbkPath)