简体   繁体   中英

Dynamically dimensioning arrays in multiple worksheets in Excel VBA?

I have 6 worksheets, each has a subcategory of data (it is important they are in separate worksheets). I am loading up the data into arrays because there are thousands of rows, then printing them out in a specific format to a .txt file.

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

I cut it off at the prining and at 2 for next loops. (Not actually sure if the for..next loops are structured properly). Anyways, my question is, am I redimensioning wrong? It gives me 'subscript out of range' error on the second attribute. Is the line

ReDim attributes2(1 To FOPRcolumnCount)

the issue? As it may be dimensioning the array in the original worksheet. Perhaps I should define the arrays in separate or worksheet models? Can I and how would I reference them? Is there a way to make the array specifically refer to a worksheet?

Appreciate any input. It's really hard not having anyone around who can provide a second opinion.

Try replacing 'FOPRcolumnCount' with an actual value. If it solves your problem then the issue is with how 'FOPRcolumnCount' is calculated, which I think is where your problem lies. It's hard to tell from your example, but it appears you are trying to find the right most column on row#1 ; there are easier ways of doing that. Same with rowCount.

I notice you haven't declared it as a variable. Always declare your variables; put "Option Explicit" at the top of your module to force you to declare all variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM