如何在VBA的共享点上引用存储在Excel文件中的单元格值

时间:2019-02-07 12:50:33

标签: excel vba sharepoint

出于对文件进行版本控制的目的,我希望能够运行一个脚本,该脚本将工作表VC上的单元格A1与运行脚本时存储在Sharepoint上的版本的同一单元格/工作表进行比较。使用VBA相当陌生,无法解决该问题,也无法在Google上找到答案。

我要使用的代码:

Public Sub version_control()

Sheets("VC").Calculate

If Sheets("VC").Range("A1").Value <> (this is where I want it to check cell A1 sheet VC on the Sharepoint file)

MsgBox "Please download the latest version from the Sharepoint"

Application.Quit
End If

End Sub

1 个答案:

答案 0 :(得分:0)

猜想,您尚未打开SharePoint文件...如果是的话,请直接跳过。

但是,如果它是打开的,您可以像其他任何打开的工作簿一样引用它...例如这两个都应该起作用:

debug.Print Workbooks("MySharePointWorkbook.xlsx").Sheets("VC").Range("A1").Value
debug.Print Workbooks.Item(n).Sheets("VC").Range("A1").Value

可能还没有打开吗?无需深入研究外部数据链接,我只需获取SharePoint文件的完整URL(在即时窗口中将其打开,? Activeworkbook.FullName),然后将该字符串存储在serverFileName中,如下所示:

Public Sub version_control()
    Dim serverFileName As String 'obtain url for sharepoint filename, insert below
    Dim valuesAreDifferent As Boolean 'so we can do housekeeping below
    Dim x As New Excel.Application 'make a new session for the sharepoint version
    Dim w As Workbook 'grab-handle for the sharepoint file
    Sheets("VC").Calculate
    valuesAreDifferent = False 'implicit, being explicit
    serverFileName = "http://whatever-domain.com/MySharepointWorkbook.xlsx"
    x.Visible = False 'so it doesn't flash up when checking
    Set w = x.Workbooks.Open(serverFileName) 'open the sharepoint version
    If Sheets("VC").Range("A1").Value <> w.Sheets("VC").Range("A1").Value Then _
        valuesAreDifferent = True
    'housekeeping in case we don't quit
    w.Close
    x.Quit
    Set w = Nothing
    Set x = Nothing
    If valuesAreDifferent Then
        MsgBox "Please download the latest version from the Sharepoint"
        Application.Quit
    End If
End Sub