在Excel VBA中dynamic地标注多个工作表中的数组?

我有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) 

问题? 因为它可能是原始工作表中的数组的大小。 也许我应该在单独或工作表模型中定义数组? 我可以和我将如何参考他们? 有没有办法让数组专门引用工作表?

欣赏任何input。 没有任何人可以提供第二个意见是很难的。

尝试用实际值replace“FOPRcolumnCount”。 如果它解决了你的问题,那么问题是如何计算“FOPRcolumnCount”,我认为这是你的问题所在。 从你的例子很难说,但你似乎正在试图find行#1最右边的列; 有更简单的方法来做到这一点。 和rowCount一样。

我注意到你还没有把它作为一个variables来声明。 总是声明你的variables; 在你的模块的顶部放置“Option Explicit”来强制你声明所有的variables。