我在共享网络驱动器上有两个工作簿:
我试图在打开Workbook B时运行宏并执行此操作:
到目前为止,在大多数情况下,下面的代码按预期工作,但如果其他人尝试在其他计算机上打开工作簿A的同时在其计算机上打开工作簿B,则会将工作簿A作为只读文件打开并在计算机上保持打开状态。我需要它在他们的计算机上关闭,并保持在另一台计算机上的初始打开。
Public Sub RefreshPvt()
ThisWorkbook.Worksheets("Sheet1").Unprotect
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wkb As Workbook
If IsFileOpen("S:\\Testing\Job Closeout Status Test.xlsx") Then
ThisWorkbook.RefreshAll
Else
Set wkb = Workbooks.Open(filename:="S:\\Testing\Job Closeout Status Test.xlsx")
ThisWorkbook.RefreshAll
wkb.Close SaveChanges:=False
End If
ThisWorkbook.Worksheets("Sheet1").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
答案 0 :(得分:0)
如果您在立即窗口中运行ThisWorkbook.RefreshAll on workboook B它是否有效?
你也可以让XLApp.Visible = true看看它是否开启
我认为你应该使用更像的东西:
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Next pt
要完成您可以使用的整个工作簿:
Sub AllWorkbookPivots()
Dim pt As PivotTable
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
End Sub
答案 1 :(得分:0)
Microsoft Office使用在与原始文件相同的目录中创建的文件来跟踪这些锁。文件filename.xlsx获取一个临时锁文件:〜$ filename.xlsx。如果文档正在使用中,则其他用户/宏将文档以只读方式打开。这是设计的。
我们可能会尝试使用Excel的共享工作簿功能,但存在一些限制。
有关更多详细信息,请参阅以下链接: