gpt4 book ai didi

arrays - 在 Excel VBA 中的多个工作表中动态标注数组?

转载 作者:行者123 更新时间:2023-12-04 21:00:41 28 4
gpt4 key购买 nike

我有 6 个工作表,每个工作表都有一个数据子类别(重要的是它们位于单独的工作表中)。我正在将数据加载到数组中,因为有数千行,然后以特定格式将它们打印到 .txt 文件中。

Sub ExcelToXML()

Dim headers(), data(), attributes1(), attributes2(), attr$, r&, c&
Dim rowCount As Long
Dim columnCount As Long
Dim FF As Worksheet
Dim FOPR As Worksheet
Dim R1 As Long
Dim C1 As Long

Set FF = Worksheets("Fairy")
Set FOPR = Worksheets("Opera")

rowCount = (FF.Range("A1048576").End(xlUp).Row) 'Only one defined as rowCount should be consistent

ffcolumncount = (FF.Range("XFD1").End(xlToLeft).Column)
FOPRcolumnCount = FOPR.Range("XFD1").End(xlToLeft).Column


' load the headers and data to an array '
FFheaders = Cells(1, 1).Resize(1, ffcolumncount).Value
FFdata = Cells(1, 1).Resize(rowCount, ffcolumncount).Value

FOPRheaders = Cells(1, 1).Resize(1, FOPRcolumnCount).Value
FOPRdata = Cells(1, 1).Resize(rowCount, FOPRcolumnCount).Value


' set the size for the attributes based on the columns per child, dynamic
ReDim attributes1(1 To ffcolumncount)
ReDim attributes2(1 To FOPRcolumnCount)

' open file and print the header two main parents
Open "C:\desktop\ToGroup.xml" For Output As #1 'file path is here, going to change to save prompt
Print #1, "<Parent>"
Print #1, " <Child>"

' iterate each row non inclusive of headers
For r = 2 To UBound(FFdata)

' iterate each column '
For c = 1 To UBound(FFdata, 2)
' build each attribute '
attr = FFheaders(1, c) & "=""" & FFdata(r, c) & """"
attributes1(c) = FFheaders(1, c) & "=""" & FFdata(r, c) & """"

Next

For R1 = 2 To UBound(FOPRdata)

For C1 = 1 To UBound(FOPRdata, 2)

attr = FOPRheaders(1, c) & "=""" & FOPRdata(r, c) & """"
attributes2(c) = FOPRheaders(1, c) & "=""" & FOPRdata(r, c) & """"
Next

我在打印时将其切断,并在 2 时将其切断以进行下一个循环。 (实际上不确定 for..next 循环的结构是否正确)。无论如何,我的问题是,我重新调整尺寸错了吗?它给了我第二个属性的“下标超出范围”错误。是线
ReDim attributes2(1 To FOPRcolumnCount)

问题?因为它可能会在原始工作表中对数组进行标注。也许我应该在单独的或工作表模型中定义数组?我可以以及如何引用它们吗?有没有办法让数组专门引用工作表?

感谢任何输入。周围没有可以提供第二意见的人真的很难。

最佳答案

尝试用实际值替换“FOPRcolumnCount”。如果它解决了您的问题,那么问题在于如何计算“FOPRcolumnCount”,我认为这就是您的问题所在。从您的示例中很难看出,但您似乎正在尝试在 row#1 上找到最右边的列;有更简单的方法可以做到这一点。与 rowCount 相同。

我注意到您尚未将其声明为变量。总是声明你的变量;将“Option Explicit”放在模块顶部以强制您声明所有变量。

关于arrays - 在 Excel VBA 中的多个工作表中动态标注数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37178983/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com