阅: 475 | 回: 5
等级:学有小成
- 积分:21
- 财富值:2
- 身份:普通用户
你是最棒的
等级:学有小成
- 积分:21
- 财富值:2
- 身份:普通用户
Sub 删除文件夹中所有工作簿的查询()
Dim 文件夹路径 As String
Dim 文件名 As String
Dim 工作簿 As Workbook
Dim 查询 As Object
Dim 连接 As WorkbookConnection
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "请选择包含Excel文件的文件夹"
If .Show = -1 Then
文件夹路径 = .SelectedItems(1)
Else
Exit Sub
End If
End With
文件名 = Dir(文件夹路径 & "\*.xls*")
Do While 文件名 <> ""
Set 工作簿 = Workbooks.Open(文件夹路径 & "\" & 文件名, ReadOnly:=False)
On Error Resume Next
For Each 查询 In 工作簿.Queries
查询.Delete
Next 查询
On Error GoTo 0
For Each 连接 In 工作簿.Connections
连接.Delete
Next 连接
工作簿.Close SaveChanges:=True
文件名 = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
MsgBox "文件夹中的所有Power Query查询已删除", vbInformation
End Sub
Dim 文件夹路径 As String
Dim 文件名 As String
Dim 工作簿 As Workbook
Dim 查询 As Object
Dim 连接 As WorkbookConnection
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "请选择包含Excel文件的文件夹"
If .Show = -1 Then
文件夹路径 = .SelectedItems(1)
Else
Exit Sub
End If
End With
文件名 = Dir(文件夹路径 & "\*.xls*")
Do While 文件名 <> ""
Set 工作簿 = Workbooks.Open(文件夹路径 & "\" & 文件名, ReadOnly:=False)
On Error Resume Next
For Each 查询 In 工作簿.Queries
查询.Delete
Next 查询
On Error GoTo 0
For Each 连接 In 工作簿.Connections
连接.Delete
Next 连接
工作簿.Close SaveChanges:=True
文件名 = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
MsgBox "文件夹中的所有Power Query查询已删除", vbInformation
End Sub
你是最棒的