从工作簿中提取.value时类型不匹配

时间:2013-11-15 19:43:25

标签: excel vba excel-vba excel-2010

我试图设置我刚刚创建的仪表板(好吧,一旦它再次打开)更新自己,因为我将来会做出更改。我已经制定了以下方法,但我仍然遇到了类型不匹配的问题。标有**的行上的错误。我觉得我在这里错过了一些非常明显的东西,但对于我的生活,我无法弄明白。有什么想法吗?

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

2 个答案:

答案 0 :(得分:3)

我认为这是因为您错误地使用了工作簿对象。

您可以在此处设置工作簿..

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

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

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

答案 1 :(得分:2)

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

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

您应该像以下一样使用它:

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