VBA代码可将数据范围从SharePoint中的关闭文件复制到打开文件

时间:2018-10-10 19:34:51

标签: excel vba

这个问题已经在不同的聊天中提出,我试图结合各种解决方案,但没有成功。我正在尝试打开SharePoint文件并将数据从文件复制到包含宏的打开文件中。以下解决方案是我最后一次尝试打开SharePoint文件但未选择所需范围的尝试。返回了运行时错误“ 9”下标超出范围。如果更容易,我的目标是将整个工作表从关闭的文件复制到打开的文件中。使用的代码如下。任何方向和建议都值得赞赏。

Price

2 个答案:

答案 0 :(得分:0)

Bruce是正确的,建议在单元格中仅使用Hyperlink。之前曾尝试过此操作,但我必须未正确链接。

答案 1 :(得分:0)

看看这是否适合您(包括注释)。

Sub OpenAndCloseWBFromSharePointFolder()

'If nobody has the file checked out
If Workbooks.CanCheckOut("http://excel-pc:43231/Shared Documents/ExcelList.xlsb") = True Then
Application.DisplayAlerts = False

'Open the file on the SharePoint server
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb", UpdateLinks:=xlUpdateLinksNever

'Close the workbook
Workbooks("ExcelList.xlsb").Close
Application.DisplayAlerts = True
Else
Application.DisplayAlerts = False

'Open the File to check if you already have it checked out
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb", UpdateLinks:=xlUpdateLinksNever

'See if doc can be checked in
If Application.Workbooks("ExcelList.xlsb").CanCheckIn Then

'Check In, Save and Close
Application.Workbooks("ExcelList.xlsb").CheckIn SaveChanges:=True, Comments:="Checked-In before Delete"

'Open the file again
Workbooks.Open Filename:="http://excel-pc:43231/Shared Documents/ExcelList.xlsb"

'Close the workbook
Workbooks("ExcelList.xlsb").Close
End If
End If

End Sub