如何为 Power Query 连接设置只读?

问题描述 投票:0回答:3

如何在 Power Query 中强制“只读”

  • 或者将源文件另存为“共享拒绝写入”?

每天我的 Excel 报告都会连接来自多个不同来源的信息,并将结果保存在单独的 xlsx 文件中。然后,这些 xlsx 文件(工作表)将用作其他报告的源。

但问题是,如果有人打开其中使用了 Power Query 连接的其他报告之一。它还将使源文件忙于更新。 (用下面我的另存为宏覆盖)
结果是没有一个报告是最新的 - 因为没有简单的方法可以将查询或连接设置为其源上的只读。

在早期的 Excel 版本中,用户可以选择 souse 连接为只读,如下所示:[连接到工作簿]https://i.sstatic.net/2L72p.jpg

我保存表格的代码如下:

Sub Sap_Ordrer_SaveAs()
     
Dim wb As Workbook
Application.DisplayAlerts = False
     
    ' SaveAs File : Sap-Ordrer.xlsx
    Sheets("SAP-ordrer").Copy
    Set wb = ActiveWorkbook
    With wb
        .SaveAs GetWorkingPath() & "\Sap-Ordrer"
        .Close False
    End With
    Set wb = Nothing

    
Application.DisplayAlerts = True
End Sub

excel vba powerquery
3个回答
0
投票

不知道这是否有帮助,但我在某个地方保存了 VBA 代码来终止 powerquery 连接。不是我的马戏团,不是我的猴子,所以无法回答有关它的问题

Dim qr As WorkbookQuery
On Error Resume Next
For Each qr In csvWrapperWB.Queries
    qr.Delete
Next
csvWrapperWB.Close Savechanges:=False

0
投票

我今天将测试这是否能解决问题 - 如果这将释放我的 xlsx 源文件进行更新,我将将此答案称为已解决。

在这里找到答案

Dim conn As Variant

For Each conn In ActiveWorkbook.Connections
    conn.OLEDBConnection.MaintainConnection = False
Next conn

更多信息可以在这里找到: 微软学习

Whola - 这有效!我将在这里介绍它在我的代码中的使用方式:

Sub TestEditWorkCenterQueries()
' Coded by Svein Arne Hylland 25.11.2022.
' This sub is to show how to change filters in all query
' In this case there is two queries one for 'Order' and one for 'Order Operations' - Bouth can filter on 'Main WorkCtr'.
' The WorkCenterArrey string will then be parsed from other sub in a form to change the filters on ThisWorkbook.Queries.
' Then each user can select what 'Main WorkCtr' they vant to include in their workbook.
Dim WorkCenterArrey As String
WorkCenterArrey = "'1338-XT','1338TOOL'"
EditAllWorkbookFormuals (WorkCenterArrey)
End Sub

Sub EditAllWorkbookFormuals(SelectedWorkCenters As String)
For Each q In ThisWorkbook.Queries
If q.Name <> "ZIQ09" Then
 q.Formula = NewQuery(q.Formula, SelectedWorkCenters)
 With ThisWorkbook.Connections("Query - " & q.Name)
    .Refresh
    .OLEDBConnection.MaintainConnection = False
 End With
End If
Next
End Sub

Function NewQuery(MyQuery As String, WorkCenterArrey As String) As String
' Function recive the set Formula and returns the updated Formula with the new filter
WorkCenterArrey = Replace(WorkCenterArrey, "'", "")
' Convert the string to arrey for filter
Dim MyArr() As String
MyArr = Split(WorkCenterArrey, ",")
' Do you manipulation here of your own MyQuery string
'MsgBox MyQuery
' Use Select case, If, Inst, replace, and so on.
' .....
Pos1 = InStr(1, MyQuery, "each ([Main WorkCtr] = ") ' Find Start position to be replaced
If Pos1 > 0 Then Pos2 = InStr(Pos1, MyQuery, ")") ' Finds then End posistion to be replaced
If Pos1 > 0 Then
    ToBeReplaced = Mid(MyQuery, Pos1, Pos2 - Pos1 + 1)
    'MsgBox ToBeReplaced
    ' Build up the new filter from Arrey
    NewFilter = "each ("
    For e = LBound(MyArr) To UBound(MyArr)
        NewFilter = NewFilter & "[Main WorkCtr] = """ & MyArr(e) & """ or "
    Next
    'Remove the last or statement - and add the parantese
    NewFilter = Left(NewFilter, Len(NewFilter) - 4) & ")"
    'MsgBox NewFilter
    
    ' Replace the filter
    MyQuery = Replace(MyQuery, ToBeReplaced, NewFilter)
End If
' Parse the new string back to Sub as the NewQuery
NewQuery = MyQuery
End Function

0
投票

一个数据密集型的折衷方案是每天创建一个新的文件名,以便您的用户习惯于在某个目录中查找文件名中具有最新日期的文件;当他们这样做时,他们会跳过许多其他文件。

另一种方法是指示您的用户根本不要打开文件,而是使用此处的方法之一将数据导入到他们自己的工作表中,或者只是在其电子表格中进行单元格引用。

© www.soinside.com 2019 - 2024. All rights reserved.