我目前使用以下 vba 将 12000+ xml 文件导入到 excel 工作簿中:
Public Sub XMLIMport()
Dim lngRow As Long
Dim strXML As String
Dim ct As Integer, XMLMap
Const maxXMLDel = 1
lngRow = 2
Do While Cells(lngRow, 1) <> ""
strXML = Cells(lngRow, 1)
ActiveWorkbook.XMLIMport url:=strXML, _
ImportMap:=Nothing, Overwrite:=False, _
Destination:=Range("$B$" & lngRow)
lngRow = lngRow + 1
For Each XMLMap In ActiveWorkbook.XmlMaps
XMLMap.Delete
Next
Loop
End Sub
然后继续将工作簿表从 excel 导入到 access 2007 中的表中。导入使用到服务器的链接来下载各个文件。
所以在 A 列中是 xml 链接,宏将 xml 数据结果放在 B、C、D 等列中,然后继续到下一行并为每个文件重复该过程。但是有这么多文件,需要相当长的时间才能完成该过程。我什至包括删除 xml 映射以加快该过程,但 Excel 2007 仍然需要相当长的时间才能完成该过程(2 小时以上)。
我猜这个宏是因为我想要的没有更好的词“优化”,还是应该有另一种方法?
更新:禁用屏幕更新但在尝试删除连接时遇到错误。
运行宏记录器我得到以下删除一些连接的信息,但我不确定如何将其添加到上述宏中以使其在继续下一个文件之前删除 xmlmap 之外的连接。
Sub deleteconnection()
'
' deleteconnection Macro
'
'
ActiveWorkbook.Connections("itemResponse").Delete
ActiveWorkbook.Connections("itemResponse1").Delete
ActiveWorkbook.Connections("itemResponse10").Delete
ActiveWorkbook.Connections("itemResponse100").Delete
ActiveWorkbook.Connections("itemResponse101").Delete
ActiveWorkbook.Connections("itemResponse102").Delete
ActiveWorkbook.Connections("itemResponse103").Delete
ActiveWorkbook.Connections("itemResponse104").Delete
ActiveWorkbook.Connections("itemResponse105").Delete
ActiveWorkbook.Connections("itemResponse106").Delete
ActiveWorkbook.Connections("itemResponse107").Delete
ActiveWorkbook.Connections("itemResponse108").Delete
ActiveWorkbook.Connections("itemResponse109").Delete
ActiveWorkbook.Connections("itemResponse11").Delete
ActiveWorkbook.Connections("itemResponse110").Delete
ActiveWorkbook.Connections("itemResponse111").Delete
ActiveWorkbook.Connections("itemResponse112").Delete
ActiveWorkbook.Connections("itemResponse113").Delete
ActiveWorkbook.Connections("itemResponse114").Delete
ActiveWorkbook.Connections("itemResponse115").Delete
ActiveWorkbook.Connections("itemResponse116").Delete
ActiveWorkbook.Connections("itemResponse117").Delete
ActiveWorkbook.Connections("itemResponse118").Delete
ActiveWorkbook.Connections("itemResponse119").Delete
End Sub
编辑:尝试使用以下内容为连接添加删除
Public Sub XMLIMport()
Dim lngRow As Long
Dim strXML As String
Dim ct As Integer, XMLMap
Dim QTable As QueryTables
'Application.ScreenUpdating = False
Const maxXMLDel = 1
lngRow = 2
Do While Cells(lngRow, 1) <> ""
strXML = Cells(lngRow, 1)
ActiveWorkbook.XMLIMport url:=strXML, ImportMap:=Nothing, Overwrite:=False, Destination:=Range("$B$" & lngRow)
lngRow = lngRow + 1
For Each XMLMap In ActiveWorkbook.XmlMaps
XMLMap.Delete
Next
For Each QTable In ActiveSheet.QueryTables
QTable.Delete
Next
Loop
'Application.ScreenUpdating = True
End Sub
导致连接仍留在工作簿中,并重新插入第一个文件