你可以
一)創建一個XML文件通過所有表循環,
二)通過加載方法打開它,
C)執行簡單的XPath搜索(我可以給後來一些例子)
我修改了一個最近的答案(參見excel-vba-xml-parsing-performance) 做一步「一)」使用後期綁定從而 一)avoidi參考最新的MS XML Version Version 6(msxml6.dll)和 b)通過所有xheets獲取數據。 XML允許您通過XPath進行結構化搜索,使邏輯結構中的節點與HTML相媲美。本例中的根節點稱爲數據,下面的節點用表名命名,隨後的節點在每個表的A行:A中獲取名稱。
一個XML文件是一個簡單的文本文件,你可以通過一個文本編輯器打開。首先您可以使用VBA XMLDOM方法分析或搜索項目(節點)。我會給你舉例說明你的問題,但給我一些時間。 =>參見答案「用法示例」,我也解釋了XML的一些優點(@Peh)。
請注意添加的註釋。
Option Explicit
Sub xmlExportSheets()
' Zweck: XML Export over all sheets in workbook
' cf. Site: [excel-vba-xml-parsing-performance][1][https://stackoverflow.com/questions/40986395/excel-vba-xml-parsing-performance/40987237#40987237][1]
' Note: pretty printed raw output with line breaks and indentation using an embedded XSLT stylesheet
On Error GoTo ErrHandle
' A. Declarations
' 1 DECLARE XML DOC OBJECT '
' a) Early Binding: VBA REFERENCE MSXML, v6.0 necessary'
' Dim doc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60, newDoc As New MSXML2.DOMDocument60
' Dim root As IXMLDOMElement, dataNode As IXMLDOMElement, datesNode As IXMLDOMElement, namesNode As IXMLDOMElement
' b) Late Binding XML Files:
Dim doc As Object
Dim xslDoc As Object
Dim newDoc As Object
' c) Late Binding XML Nodes:
Dim root As Object
Dim sh As Object ' xml node containing Sheet Name
Dim dataNode As Object
Dim datesNode As Object
Dim namesnode As Object
' 2 DECLARE other variables
Dim i As Long
Dim j As Long
Dim tmpValue As Variant
Dim tit As String
Dim ws As Worksheet
' B. XML Docs to Memory
Set doc = CreateObject("MSXML2.Domdocument.6.0")
Set xslDoc = CreateObject("MSXML2.Domdocument.6.0")
Set newDoc = CreateObject("MSXML2.Domdocument.6.0")
' C. Set DocumentElement (= root node)'
Set root = doc.createElement("data")
' D. Create Root Node
doc.appendChild root
' ===========================
' ITERATE THROUGH Sheets
' ===========================
For Each ws In ThisWorkbook.Sheets
Set sh = doc.createElement(ws.Name) '
root.appendChild sh
' ===========================
' ITERATE THROUGH ROWS ' A2:NNn
' ===========================
For i = 2 To ws.UsedRange.Rows.Count ' Sheets(1)
' DATA ROW NODE '
Set dataNode = doc.createElement("row") '
sh.appendChild dataNode
' TABLES NODE (orig.: DATES NODE) '
Set datesNode = doc.createElement(ws.Cells(1, 1)) ' Dates
datesNode.Text = ws.Range("A" & i)
dataNode.appendChild datesNode
' NAMES NODE '
For j = 1 To ws.UsedRange.Columns.Count - 1 ' = 12
tit = ws.Cells(1, j + 1)
tmpValue = ws.Cells(i, j + 1)
Set namesnode = doc.createElement(tit)
namesnode.Text = tmpValue
dataNode.appendChild namesnode
Next j
Next i
Next ws
' =============================
' PRETTY PRINT RAW OUTPUT (XSL)
' =============================
xslDoc.LoadXML "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" _
& "<xsl:stylesheet version=" & Chr(34) & "1.0" & Chr(34) _
& " xmlns:xsl=" & Chr(34) & "http://www.w3.org/1999/XSL/Transform" & Chr(34) & ">" _
& "<xsl:strip-space elements=" & Chr(34) & "*" & Chr(34) & " />" _
& "<xsl:output method=" & Chr(34) & "xml" & Chr(34) & " indent=" & Chr(34) & "yes" & Chr(34) & "" _
& " encoding=" & Chr(34) & "UTF-8" & Chr(34) & "/>" _
& " <xsl:template match=" & Chr(34) & "node() | @*" & Chr(34) & ">" _
& " <xsl:copy>" _
& " <xsl:apply-templates select=" & Chr(34) & "node() | @*" & Chr(34) & " />" _
& " </xsl:copy>" _
& " </xsl:template>" _
& "</xsl:stylesheet>"
' XSLT (Transformation)
xslDoc.async = False
doc.transformNodeToObject xslDoc, newDoc
' =================
' Save the XML File
' =================
newDoc.Save ThisWorkbook.Path & "\Output.xml"
MsgBox "Successfully exported Excel data to " & ThisWorkbook.Path & "\Output.XML!", vbInformation
' Regular End of procedure
Exit Sub
ErrHandle:
MsgBox Err.Number & " - " & Err.Description, vbCritical
Exit Sub
末次
注意
工作表名稱必須是不帶空格
新增注(重要提示): XML節點使用的標題中的每一個第一排片。由於修改的過程通過UsedRange獲取標題名稱,因此在行A中不要有任何空單元格:本例中爲A。
補充評論 我不知道爲什麼我的即時答案(標記爲「a」)被某人降級。我會覺得有幫助的爭論這個:-)
你可以使用一個循環,通過標籤1又WorksheetFunction.Match方法](HTTPS行循環: //msdn.microsoft.com/en-us/library/office/ff835873.aspx)或[Range.Find方法](https://msdn.microsoft.com/en-us/library/office/ff839746.aspx)查看當前行的其他選項卡中是否存在匹配項。同樣按保單號碼排序可以縮短循環。 –
爲什麼不使用vba'Find'功能。您可以搜索所有匹配的保單號碼(如果對保單號碼有多個借方/貸方等) – Tom
當您說標籤時,是否指列?在這種情況下數據看起來像什麼(舉例)? – Vegard