从工作簿拉取.value时出现types不匹配

我试图设置我刚刚创build的仪表板,以便在将来进行更改时自动进行更新(当然,一旦它被重新打开)。 我已经制定了下面的方法,但是我一直在标有**的行上出现“types不匹配”错误。 我觉得我在这里错过了一些非常明显的东西,但是对于我来说,我无法弄清楚,有什么想法吗?

Private Sub Workbook_Open() Dim UpDateBook As Workbook Dim CurrVer As String Dim AdminFile As String Dim AdminFolder As String Dim MyPath As String ''Change the next two according to where the admin file will be located. AdminFile = "\\dallfile\Databases\Reports\Dashboard\Dashboard Update.xlsx" AdminFolder = "\\dallfile\Databases\Reports\Dashboard" MyPath = ThisWorkbook.Path MyPath = MyPath & "\" Application.ScreenUpdating = False Set UpDateBook = Workbooks.Open(AdminFile, , True) **CurrVer = Workbooks(UpDateBook).Sheets("Version_Log").Range("A5000").End(xlUp).Value CurrVer = CurrVer & ".xlsm" If ThisWorkbook.Name <> CurrVer Then MsgBox ("There is a new update for your file available. It will be loaded as soon as you press OK") Workbooks.Open Filename:=AdminFolder & CurrVer Application.EnableEvents = False Workbooks(CurrVer).SaveAs Filename:=MyPath & CurrVer, FileFormat:=xlNormal Application.EnableEvents = True With ThisWorkbook .Saved = True .ChangeFileAccess Mode:=xlReadOnly Kill pathname:=.FullName .Close savechanges:=False End With End If Application.ScreenUpdating = True End Sub 

我认为这是因为您正在使用您的工作簿对象不正确。

你在这里设置工作簿

 Set UpDateBook = Workbooks.Open(AdminFile, , True) 

然后你应该像这样使用它。

 CurrVer = UpDateBook.Sheets("Version_Log").Range("A5000").End(xlUp).Value 

只是一个语法问题。 一旦你有:

 Set UpDateBook = Workbooks.Open(AdminFile, , True) 

你应该像这样使用它:

 CurrVer = UpDateBook.Sheets("Version_Log").Range("A5000").End(xlUp).Value