在随机物品中订阅超出范围的错误

我的代码应该从列表框中取出项目并将它们分开,以便在表格中的表格中build立它。 怪异的部分不是它一直抛出

订阅超出范围

它是抛出它随机的具体项目,我尝试使用具有相似的特点,但他们的工作不同的项目。 所有这些项目来自同一张表。 我希望有人看我的代码,以确保它是我的代码,这不是我错过了一个字的特点

这是错误发生的地方:

.Range("A" & (i + 1)).Value = strsplt(i) 

这是整个代码:

 Dim i As Long, j As Long, ii As Long Dim found As Boolean Dim str As String Dim message, title, defaultval As String Dim quantity As String Dim strsplt() As String ReDim strsplt(0 To i) 'Seperate Items and put them in table with quantity needed With Me.selecteditems ThisWorkbook.Sheets(9).Range("A:B").ClearContents For i = 0 To .ListCount - 1 If .Selected(i) Then found = True For ii = 0 To .ColumnCount - 1 If str = "" Then str = .List(i, ii) & vbTab Else If ii < .ColumnCount - 1 Then str = str & .List(i, ii) & vbTab Else str = str & .List(i, ii) End If End If Next ii message = "How much" & vbCrLf & str & "?" title = "Amount" defaultval = "1" quantity = InputBox(message, title, defaultval) strsplt() = Split(str, " ") End If 'On Error Resume Next With ThisWorkbook.Sheets(9) .Range("A" & (i + 1)).Value = strsplt(i) .Range("B" & (i + 1)).Value = quantity End With 'On Error GoTo 0 Next i End With 

 ReDim strsplt(0 To i) 

当该指令运行时, i值为0 ; ReDim声明实际上是无用的/多余的。

然后你分配数组:

 strsplt() = Split(str, " ") 

括号是多余的,但这不是问题:问题是,如果str不能被分割成使用该分隔符的i元素,那么这个:

 .Range("A" & (i + 1)).Value = strsplt(i) 

从数组中拉出超出界限的值。

我很乐意帮助你简化你的代码,但逻辑是相当复杂的,我不能在这上面花一个小时。 学习放置断点(F9),逐步执行代码(F8)并检查局部variables值(使用本地工具窗口,或在直接窗格中使用? / Print语句,或者将variableshover在break模式中)应该很快find解决办法。