[英]Excel VBA Subscript out of range error in array when assigning values to array
我正在研究一個類似的數據集,如下所示:
我正在按照列作業循環輸入數據,並試圖找出為該作業映射的繼任者。 我正在將繼任者和退休年份分配給我的二維數組的帖子。 發布此我正在嘗試將數組分配給列表框。 這是我的代碼:
Dim hjselected As Variant
hjselected = frmform1.ComboBox1.Value
iRow = [Counta(Database_HJ!A:A)] - 1
Dim arrayrow As Variant
arrayrow = 0 'variable for array row size
For countRow = 2 To iRow 'to find array row size I need based on number of incumbent
If (hjselected = Sheet2.Cells(countRow, 1)) Then
arrayrow = arrayrow + 1
End If
Next countRow
Dim varA() As Variant
ReDim varA(arrayrow, 6)
For countRow = 2 To iRow
If (hjselected = Sheet2.Cells(countRow, 1)) Then
varA(countRow, 1) = Sheet2.Cells(countRow, 9)
varA(countRow, 2) = Sheet2.Cells(countRow, 10)
varA(countRow, 3) = Sheet2.Cells(countRow, 11)
varA(countRow, 4) = Sheet2.Cells(countRow, 12)
varA(countRow, 5) = Sheet2.Cells(countRow, 13)
varA(countRow, 6) = Sheet2.Cells(countRow, 14)
End If
Next countRow
frmform1.ListBox1.List = varA
當第二個 for 循環第二次運行時,我不斷收到下標超出范圍錯誤。 任何幫助將不勝感激,非常感謝。
我正在使用 countrow 向數組添加值,但 countrow 將具有更大的值。 我又用了一個櫃台繞過它。
Dim arrayI As Long
arrayI = 1
For countRow = 2 To iRow
If (hjselected = Sheet2.Cells(countRow, 1)) Then
varA(arrayI, 1) = Sheet2.Cells(countRow, 9)
varA(arrayI, 2) = Sheet2.Cells(countRow, 10)
varA(arrayI, 3) = Sheet2.Cells(countRow, 11)
varA(arrayI, 4) = Sheet2.Cells(countRow, 12)
varA(arrayI, 5) = Sheet2.Cells(countRow, 13)
varA(arrayI, 6) = Sheet2.Cells(countRow, 14)
arrayI = arrayI + 1
End If
Next countRow
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.