2016-03-31 75 views
2

下面是構建屬性然後將它們打印爲xml文件的代碼。每次運行它時,都會說下標超出範圍,突出顯示attributes2。第40-41列應該在定義的範圍內。在Excel VBA中構建數組時,下標超出範圍?

我認爲這個問題可能是因爲我不應該使用'ElseIf'。當我只將它作爲屬性1和屬性2運行時,它在我使用'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 
+0

你問'ElseIf 40 <= CBool​​(c <= 41)'。也許你應該問'ElseIf 40 <= c和c <= 41)'...? – Jeeped

回答

3

問題是與你的If語句 - 你不能鏈條件語句一樣,在VBA。在VBA中,他們將從左向右計算的,所以這...

40 <= 50 <= 41 

...變成這樣:

(40 <= 50) <= 41 

表達40 <= 50回報True,成爲...

True <= 41 

...和真正的強制轉換成在VBA -1的數值,所以你最終...

If -1 <= 41 Then 

......恰巧是True和一個超出範圍的索引。

所以,把你的測試的And

ElseIf 40 <= c And c <= 41 Then 
+0

聖......如果這是我將要削減的答案。當我有時間對此進行測試時更新。 –

+0

完美的作品,非常感謝。 –

1

以前的答案是正確的,但作爲一種風格的東西,這是使用一個選擇.. Case語句一個完美的地方:

Select Case c 
    Case 1 To 39 
     attributes1(c) = attr 
    Case 40 To 41 
     attributes2(c) = attr 
    Case 42 To 51 
     attributes3(c) = attr 
    ... ' Fill in the other possibilities 
End Select 
+0

謝謝你提醒我,我知道解決方案非常明顯。有時候你只需要散步就可以回到新的角度...... –