我有一本工作簿,其中包含一些要通过VBA Script导出到excel的工作表。我要导出的工作表中有一个数据库连接以提取该报告期间的值。当我导出到excel时,我注意到与数据库的连接仍然存在。我可以使用一段代码来导出工作表中的值但删除数据库连接吗?以下是我目前每天用于导出报告的脚本。谢谢!
Sub refreshsummary()
' refreshsummary Macro
Dim strdate As String
'Refresh Data Summary View
Sheets("Data").Select
Range("b1").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Fill Down Formula for Summary Data
Sheets("Data").Select
Range("A2:A10000" & LastRow).Formula = "=B2&"" ""&C2&"" ""&E2&"" ""&F2&"" ""&G2&"" ""&D2"
'Refresh Data Export View
Sheets("Data Export").Select
Range("a1").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheets("Control").Select
strdate = Format(Range("c2").Value, "mmm-dd-yyyy")
ActiveWorkbook.Save
'excel read only
Application.DisplayAlerts = False
Sheets(Array("Template", "Data Export", "Sales Breakdown")).Copy
Dim ExternalLinks As Variant
Dim x As Long
'Create an Array of all External Links stored in Workbook
ExternalLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
'Loop Through each External Link in ActiveWorkbook and Break it
For x = 1 To UBound(ExternalLinks)
ActiveWorkbook.BreakLink Name:=ExternalLinks(x), Type:=xlLinkTypeExcelLinks
Next x
'Removes Formulas
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
With sh.Cells
.Copy
.PasteSpecial xlPasteValuesAndNumberFormats
End With
Next sh
ActiveWorkbook.SaveAs Filename:="MYFILE.xlsx", FileFormat:=51, CreateBackup:=False
'End If
End Sub
答案 0 :(得分:1)
也许遵循以下原则:
{
'Google Maps': 1,
'Facebook for Android': 1,
'Pandora?? internet radio': 1,
'Zedge Ringtones & Wallpapers': 1,
'Advanced Task Killer': 1,
'Twitter': 1,
'Tiny Flashlight + LED': 1,
'GO SMS Pro': 1,
'The Weather Channel': 1,
'Shazam': 1,
'Lookout Security & Antivirus': 1,
'YouTube': 1,
'Dictionary.com': 1,
'TuneIn Radio': 1,
'Movies': 1,
'ColorNote Notepad Notes': 1,
'Antivirus Free': 1,
'Bible': 1,
'TiKL': 1...}
在您的情况下,您似乎需要使用With Workbooks("target workbook name")
For i = 1 To .Connections.Count
.Connections(1).Delete
Next
End With
。请注意,删除索引With ActiveWorkbook
最终会出错,因为删除操作将更改正在迭代的集合:i
最终将大于集合的大小。
或者:
i