【问题标题】:How to filter data in VBA?如何在 VBA 中过滤数据?
【发布时间】:2016-06-17 15:53:26
【问题描述】:

我正在开发一个基于 VBA 的插件。我从 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 的列表,其中 Site 是 Facebook。我还想获取URLTwitter 列表。

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

【问题讨论】:

  • 您的数据当前保存在数组中;您将需要遍历它们以找到您要查找的内容。您可能需要考虑一个二维数组并将站点、名称和 url 存储在单个元素中,这样所有内容都保持紧密耦合。
  • @Dave 除了存储在数组中更适合过滤器之外,您能否建议我使用其他方法。当我刚开始工作时,我可以相应地改变我的流程。

标签: arrays vba


【解决方案1】:

根据你所说的你想要的,我已经修改了你的代码并添加了 cmets 来解释我在做什么:

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

【讨论】:

  • 非常感谢。你能建议我这个查询吗?我试过但没有找到解决办法。我想列出特定Name 和特定SiteURL。例子。 ABC 可以有 2 个Facebook 帐户。所以,列出SiteFacebookNameABCURLs
  • 我的意思是有 2 个条件的过滤器。 “Facebook”和“ABC”
  • If Instr(LCase(myArray(0,i)), "facebook") &gt; 0 And Instr(LCase(myArray(1,i)), "abc") &gt; 0 Then 将为您检查这两个条件
  • 我有 7 列。对于这些行,我将在您的代码中将 2 替换为 6:1- Public myArray(2,0) As String。 2- ReDim 保留 myArray(2, UBound(myArray,2)+1)。 3- 对于 i = 0 到 UBound(myArray, 2)。如果我错了,请告诉我。
  • 我已经编辑了答案以演示 7 列的使用。数组仍然只有 2 个维度(将它们视为行和列)
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 2017-12-07
  • 2017-02-06
  • 2023-02-02
相关资源
最近更新 更多