jjzjj

xml - 在 Excel VBA 中构建数组时下标超出范围?

coder 2024-07-05 原文

下面是构建属性然后将它们打印为 xml 文件的代码。每次我运行它,它都说下标超出范围,突出显示 attributes2。第 40-41 列应在定义的范围内。

我认为问题可能是因为我不应该使用“ElseIf”。当我仅将它作为 attributes1 和 attributes2 运行时,当我使用“Else”语句时它工作正常。也许我错误地定义了我的数组,无论哪种方式我都无法找到答案并且需要一些新的眼光。

Sub ILikeFruits()

Dim headers(), data(), attributes1(), attributes2(), attributes3(), _
attributes4(), attributes5(), attributes6(), attributes7(), attributes8(), attr$, r&, c&

' load the headers and data to an array '

headers = Cells(1, 1).Resize(1, 104).Value
data = Cells(2, 1).Resize(10, 104).Value


' set the size for the attributes '
ReDim attributes1(1 To 39)
ReDim attributes2(40 To 41)
ReDim attributes3(42 To 51)
ReDim attributes4(52 To 65)
ReDim attributes5(66 To 69)
ReDim attributes6(70 To 89)
ReDim attributes7(90 To 97)
ReDim attributes8(98 To 104)

' open file and print the header '
Open "C:\desktop\XML Update\Simulation\XML tests (Attributes)\DataTest.xml" For Output As #1
Print #1, "<Fruits>"
Print #1, "  <Tasty_Fruits>"

' iterate each row '
For r = 2 To UBound(data)

  ' iterate each column '
  For c = 1 To UBound(data, 2)

    ' build each attribute '
    attr = headers(1, c) & "=""" & data(r, c) & """"
    If c <= 39 Then
      attributes1(c) = attr
    ElseIf 40 <= c <= 41 Then 'Subscript out of range
      attributes2(c) = attr
    ElseIf 42 <= c <= 51 Then
      attributes3(c) = attr
    ElseIf 52 <= c <= 65 Then
      attributes4(c) = attr
    ElseIf 66 <= c <= 69 Then
      attributes5(c) = attr
    ElseIf 70 <= c <= 89 Then
      attributes6(c) = attr
    ElseIf 90 <= c <= 97 Then
      attributes7(c) = attr
    ElseIf 98 <= c <= 104 Then
      attributes8(c) = attr
      End If
  Next

  ' print the row '
  Print #1, "    <Fruits_By_Color " & Join(attributes1, " ") & " >"
  Print #1, "      <Small_Red_Fruits>"
  Print #1, "        <Cranberry " & Join(attributes2, " ") & " />"
  Print #1, "      </Small_Red_Fruits>"
  Print #1, "      <Big_Red_Fruits>"
  Print #1, "        <Apple " & Join(attributes3, " ") & " />"
  Print #1, "        <Pomegranate " & Join(attributes4, " ") & " />"
  Print #1, "        <Tomato " & Join(attributes5, " ") & " />"
  Print #1, "      </Big_Red_Fruits>"
  Print #1, "      <Yellow_Fruits>"
  Print #1, "       <Small_Yellow_Fruits " & Join(attributes6, " ") & " >"
  Print #1, "        <Banana " & Join(attributes7, " ") & " />"
  Print #1, "        <Lemon " & Join(attributes8, " ") & " />"
  Print #1, "       </Small_Yellow_Fruits>"
  Print #1, "      </Yellow_Fruits>"
  Print #1, "    </Fruits_By_Color>"

Next

' print the footer and close '
Print #1, "  </Tasty_Fruits>"
Print #1, "</Fruits>"
Close #1

End Sub

最佳答案

问题出在你的If上语句 - 您不能像 VBA 中那样链接条件。在 VBA 中,它们将从左到右计算,所以这...

40 <= 50 <= 41

...变成这样:

(40 <= 50) <= 41

表达式40 <= 50返回 True , 变成...

True <= 41

...并且 True 在 VBA 中强制转换为 -1 的数值,所以您最终得到...

If -1 <= 41 Then

...这恰好是 True以及超出范围的索引。

所以,放一个And在你的测试中:

ElseIf 40 <= c And c <= 41 Then

关于xml - 在 Excel VBA 中构建数组时下标超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36323305/

有关xml - 在 Excel VBA 中构建数组时下标超出范围?的更多相关文章

  1. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  2. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  3. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  6. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  7. ruby - 触发器 ruby​​ 中 3 点范围运算符和 2 点范围运算符的区别 - 2

    请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是

  8. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  9. ruby - 在 Ruby 中用键盘诅咒数组浏览 - 2

    我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作

  10. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

随机推荐