我创建了一个启用宏的 Excel 工作簿(我称之为 NewData.xlsm),它有一个刷新按钮,当您单击它时,它会连接到网络文件夹 (Source.xlsm) 中的源文件。 我与我的同事共享了 NewData.xlsm,他们都有一份副本存储在他们的计算机中。 我每月更新 source.xlsm 文件,我的同事通过打开他们的 NewData.xlsm 文件并在单击 NewData.xlsm 上的刷新按钮时连接到 Source.xlsm 来获取新数据。
源文件位于所有用户都可以通过 VPN 访问的网络文件夹中。
当每个人在不同时间单独尝试时效果很好。 问题发生在:
我想允许多个用户同时连接到源而不会出现问题。我知道这是可能的,因为我使用了另一个由其他人创建的 Excel (ExcelThatWorks.xlsm),它允许这样做,但我找不到让我的 NewData.xlsm 做同样事情的方法。 如果我没有做到这一点,那么用户将不得不按计划访问该文件,这是不可接受的。
我确实在 ExcelThatWorks.xlsm 文件中看到,有一段代码显示 OLEDBConnection(我在下面添加了代码),并认为这可能就是我需要的,但我不确定,因为我使用的是 VBA。
我在 Windows 10 企业版上使用 Office LTSC Professional Plus 2021。
这些是我在 NewData.xlsm 文件上的所有代码,当一个人一次使用它时它可以完美地工作,但当多人同时打开它时就不行了。
______________________________________
Option Explicit
Public Sub Stop_ScreenUpdateOpen()
Application.ScreenUpdating = False
'Open a workbook
'Open method requires full file path to be referenced.
Workbooks.Open "\\Full\Shared\Folder\Path\Source.xlsm"
Application.ScreenUpdating = True
End Sub
______________________________________
Public Sub Stop_ScreenUpdateCopyPasteRaw()
Dim s As Workbook
Dim d As Workbook
Dim vals As Variant
'## Open both workbooks first:
Set s = Workbooks.Open("\\Full\Shared\Folder\Path\Source.xlsm ")
Set d = ThisWorkbook
With s.Sheets("RAW").UsedRange
'Now, paste to d worksheet:
d.Sheets("RAW").Range("A1").Resize( _
.Rows.Count, .Columns.Count) = .Value
End With
End Sub
______________________________________
Public Sub Stop_ScreenUpdateCopyPasteData()
Dim s As Workbook
Dim d As Workbook
Dim vals As Variant
'## Open both workbooks first:
Set s = Workbooks.Open("\\Full\Shared\Folder\Path\Source.xlsm ")
Set d = ThisWorkbook
With s.Sheets("Data").Range("A2:j200")
'Now, paste to d worksheet:
d.Sheets("Data").Range("A2").Resize( _
.Rows.Count, .Columns.Count) = .Value
End With
End Sub
______________________________________
Public Sub Stop_ScreenUpdateClose()
Application.ScreenUpdating = False
'Close a workbook
Workbooks("Source.xlsm").Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
______________________________________
Public Sub RefreshConnections()
ActiveSheet.PivotTables("PivotTable1").RefreshTable
MsgBox "Data has been refreshed!"
End Sub
______________________________________
CALL Method
Public Sub Stop_ScreenUpdateUpdate()
Application.ScreenUpdating = False
Call Stop_ScreenUpdateOpen
Call Stop_ScreenUpdateCopyPasteRaw
Call Stop_ScreenUpdateCopyPasteData
Call Stop_ScreenUpdateClose
Call RefreshConnections
Application.ScreenUpdating = True
End Sub
______________________________________
我上面提到的ExcelThatWorks.xlsm文件有这样的代码:
______________________________________
Public Sub UpdatePowerQueries()
' Macro to update my Power Query script(s)
Dim lTest As Long, cn As WorkbookConnection
On Error Resume Next
For Each cn In ThisWorkbook.Connections
lTest = InStr(1, cn.OLEDBConnection.Connection, "Provider=Microsoft.Mashup.OleDb.1", vbTextCompare)
If Err.Number <> 0 Then
Err.Clear
Exit For
End If
If lTest > 0 Then cn.Refresh
Next cn
End Sub
______________________________________
它没有其他代码,只有那个并且像魅力一样工作。但我不知道如何达到相同的结果。
我检查了 在不打开文件的情况下同时由多个用户更新一个 excel 文件 和提交此问题之前呈现给我的其他线程,但都指用户编辑源文件。
我的用户没有在源文件中添加/删除任何信息,他们只是从源文件中提取新数据,不需要编辑任何内容。