VBA Workbook.Open(File)返回Nothing

编辑: 经过大量的帮助,没有一个线索怎么回事,它使用了不同的方法打开( 见@ JohnMuggin的帮助下面 ) – 所以我没有评论我的原代码,突然它的工作。

我只find另一个Workbook.Open("file")实例没有返回(链接到Q) 。 但是,他们的问题是因为在用户定义的函数(以我的理解)调用Workbook.Open("file") )。 在这里,我打电话给一个Sub ,但我有同样的问题,找不到解决scheme。 我正在使用Excel 2013。

 Private Sub CommandButton2_Click() 'Set variables Dim wb As Workbook 'Workbook to open Dim wbR As Worksheet 'This is the raw data on the new workbook Dim wsL As Worksheet 'Worksheet in current file Dim myFile As String 'File to open Dim FilePicker As FileDialog 'Set light chain hit worksheet Set wsL = ThisWorkbook.Worksheets(3) 'Optimizes Speed Application.EnableEvents = False Application.Calculation = xlCalculationManual 'Retrieve Target Folder Path From User Set FilePicker = Application.FileDialog(msoFileDialogFilePicker) 'Opens folder-picking window With FilePicker .Title = "Select a file." .AllowMultiSelect = False If .Show = True Then myFile = .SelectedItems(1) Else: GoTo NextCode End If End With 'If folder is not selected NextCode: myFile = myFile If myFile = "" Then GoTo ResetSettings 'Set variable equal to opened workbook Set wb = Workbooks.Open(myFile) 

macros继续,但最后一行Set wb = Workbooks.Open(myFile)wb设置为Nothing 。 这不会产生任何错误,直到我在代码中向下调用wb

更早的行Set wsL = ThisWorkbook.Worksheets(3)也设置wsLNothing

我使用debugging器检查了每一行和值,并确定myFile是正确的path,文件名和扩展名。

而在最后尝试这个小的子。 如果它不打开您的工作簿,则path或文件名有问题

 Sub opendfghj() Dim wb As Workbook Workbooks.Open Filename:="C:\Users\User\Desktop\Templates and Example data\Repeat Tests\file.xlsx" Set wb = ActiveWorkbook wb.Worksheets("Sheet1").Range("A1") = "It Works" End Sub