如何在VBA中过滤数据?

时间:2016-06-15 14:06:42

标签: arrays vba

我正在开发一个基于VBA的AddIn。我从XML文件导入数据。我想对该数据应用过滤器。我不知道如何存储和整理数据。

XML文件:

<?xml version="1.0"?>
<TestClass>
  <TestObject>
    <Site>Facebook</Site>
    <Name>ABC</Name>
    <URL>https://www.facebook.com/ABC/</URL>
  </TestObject>
  <TestObject>
    <Site>Facebook</Site>
    <Name>XYZ</Name>
    <URL>https://www.facebook.com/XYZ/</URL>
  </TestObject>
  <TestObject>
    <Site>Twitter</Site>
    <Name>ABC</Name>
    <URL>https://www.twitter.com/ABC/</URL>
  </TestObject>
  <TestObject>
    <Site>Facebook</Site>
    <Name>XYZ</Name>
    <URL>https://www.twitter.com/XYZ/</URL>
  </TestObject>
</TestClass>

这是我的代码。

Set oXMLFile = CreateObject("Microsoft.XMLDOM")
XMLFileName = "C:\Users\abc\Desktop\TestFiles\TestData.xml"
oXMLFile.Load (XMLFileName)

Set Sites = oXMLFile.SelectNodes("TestClass/TestObject/Site/text()")
Set Names = oXMLFile.SelectNodes("TestClass/TestObject/Name/text()")
Set URLs = oXMLFile.SelectNodes("TestClass/TestObject/URL/text()")

Public SiteArray(100) As String 
Public NameArray(100) As String 
Public URLArray(100) As String 

For i = 0 To (Sites.Length - 1)
    SiteArray(i) = Sites(i).NodeValue
    NameArray(i) = Names(i).NodeValue
    URLArray(i) = URLs(i).NodeValue
Next

现在我不知道如何获取网站Name的{​​{1}}列表。我还想获得Facebook的{​​{1}}列表。

在上述情况下,有人可以建议我过滤机制吗?

1 个答案:

答案 0 :(得分:0)

根据您所说的内容,我修改了您的代码并添加了评论以解释我的所作所为:

Set oXMLFile = CreateObject("Microsoft.XMLDOM")
XMLFileName = "C:\Users\abc\Desktop\TestFiles\TestData.xml"
oXMLFile.Load (XMLFileName)

Set Sites = oXMLFile.SelectNodes("TestClass/TestObject/Site/text()")
Set Names = oXMLFile.SelectNodes("TestClass/TestObject/Name/text()")
Set URLs = oXMLFile.SelectNodes("TestClass/TestObject/URL/text()")

Public myArray() As String  ' declare array as dynamic so we can amend its size
ReDim myArray(6,0) ' reset it to be 2 dimensional 

For i = 0 To (Sites.Length - 1)
    myArray(0,i) = Sites(i).NodeValue ' Column 1 of the array contains Sites
    myArray(1,i) = Names(i).NodeValue ' Column 2 of the array contains Names
    myArray(2,i) = URLs(i).NodeValue  ' Column 3 of the array contains URLs
    myArray(3,i) = URLs(i).NodeValue  ' Column 4 of the array contains URLs
    myArray(4,i) = URLs(i).NodeValue  ' Column 5 of the array contains URLs
    myArray(5,i) = URLs(i).NodeValue  ' Column 6 of the array contains URLs
    myArray(6,i) = URLs(i).NodeValue  ' Column 7 of the array contains URLs
    ReDim Preserve myArray(6, UBound(myArray,2)+1) ' increase the size of the array dynamically to include an extra "row" of data
Next
' Now you can loop through and search for anything you want, like so:
For i = 0 to UBound(myArray, 2) ' loop through all the "rows" of the array
    If Instr(LCase(myArray(0,i)), "facebook") > 0 Then ' If the site contains facebook
        myName = myArray(1, i) ' returns the associated name for the Site
        myURL = myArray(2, i) ' returns the associated URL for the Site
        ' Do whatever you need with this info then let the loop continue through the rest of the rows or use an Exit For statement to break the loop
    End If
Next